• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trouble Printing Hashmap Key and Values in an ArrayList  RSS feed

 
Derek Von
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to add two strings to a hash map. the first being the key, a 3 digit code that can have duplicates, and the value to store in an ArrayList. From what I've read, when add a key to the hashmap that is a duplicate, the previous gets overwritten and that is why I am trying to put the values in an array list. I was hoping that when the key is looked up, it would print all the values associated with that key:




So when I try to add values such as:
library.checkoutBook("000", "test 1");
library.checkoutBook("000", "test 2");
library.checkoutBook("001", "test 3");
library.checkoutBook("001", "test 4");
library.checkoutBook("002", "test 5");
library.checkoutBook("002", "test 6");

It prints
In print--ISBN: 000
ISBN: 000Patron: test 1
ISBN: 000Patron: test 2
ISBN: 000Patron: test 3
ISBN: 000Patron: test 4
ISBN: 000Patron: test 5
ISBN: 000Patron: test 6
In print--ISBN: 001
ISBN: 001Patron: test 1
ISBN: 001Patron: test 2
ISBN: 001Patron: test 3
ISBN: 001Patron: test 4
ISBN: 001Patron: test 5
ISBN: 001Patron: test 6
In print--ISBN: 002
ISBN: 002Patron: test 1
ISBN: 002Patron: test 2
ISBN: 002Patron: test 3
ISBN: 002Patron: test 4
ISBN: 002Patron: test 5
ISBN: 002Patron: test 6

Any help would be appreciated!
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have only one list which is patName. you should have a separate list for every isbn.
Create and put that list into Map only when you add an isbn that is not yet in a map.
If the key is present, get a list from the map and add value to the list.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
D Last wrote:I was hoping that when the key is looked up, it would print all the values associated with that key:

It does. The problem is that you have only ONE set of values (patName), and you point ALL your keys to it.

I'll let you see if you can figure out how to solve that one yourself. If you get really stuck, come back and we'll try and help you further.

Winston
 
Derek Von
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help.

After reading your advice, I did some research and found I could create a new arraylist when the key was not found. I'm sure there is a much more elegant way to implement this, but this is what I did:


Now, when I try to remove one of the values from the arraylist I get a crash. I'm not sure what is causing the crash, but it crashes after removing the item from the arraylist:


Any help figuring out the crash would be appreciated.

Thanks
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to loop in a search for key like thisUse checkoutBooks.containsKey method instead.

Also do not compare Strings using == operator. Use equals method instead.
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead ofI would write But this is just another way for writing the same thing.

Derek Von wrote:Now, when I try to remove one of the values from the arraylist I get a crash. I'm not sure what is causing the crash, but it crashes after removing the item from the arraylist:

What do you mean by crash? Does it throw an exception? Does it even compile? Post an error here.
Again. Don't loop through key set in a search for what you need.
Use Map.get method to get your ArrayList. If isbn is not in the map, you'll get null. Check for that.

Sorry for double post. Just a mistake...
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:You don't need to loop in a search for key like this...Use checkoutBooks.containsKey method instead.

Indeed.

@Derek: I suggest you take a good look at the Map API (java.util.Map ←click), because it has lots of useful methods, and you really should understand how to use them. Maps are very important structures, and you will have to deal with them quite a bit.

Also: read the AvoidTheEqualityOperator page. It explains why Pawel gave you the advice he did.

Winston
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!