This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Questions about collections  RSS feed

 
Paul Peterson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working with collections on a project and have a couple of questions.  I'll start with the one that is the easiest to describe.  Is there a way to merge two HashMaps on the key and change the key value of one of the HashMaps to keep the unique value in the key without iterating through both HashMaps?

Second.  In a different section of the same project I am working with an ArrayList of an object call it ArrayList<Product> item... I want to print two of the instance variables of the object and the count on three different lines and only four objects per line.  My first thought was a nested for loop, but that just gave me the first item 4 times.

This is what I am trying to modify to print p.getPrice() on the next line and count on the third line (yes, there is quite a bit of extraneous code in this, much of it is formatting, or my attempt at formatting)
 
Campbell Ritchie
Marshal
Posts: 55715
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are methods in most of the classes in the Collections Framework which allow you to add all the contents of one instance to the other instance. Look at the Map interface in the Java™ Tutorials, and in the API documentation. There will probably be something which does what you want.
Do you have a List<Item> or a List<List<Item>>? Unless you have the latter, I don't think your for‑each loop will compile.
Does your Item class have a suitable method for printing that format? I am afraid I don't think I can help you much more because I don't know anything about your console object. Please supply more details about it.
 
Paul Peterson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The printing portion is using an ArrayList of Products.

The "console.printOut" is the same as System.out.Print()

I have looked at some of the documentation and found some things that I understand, but do not do what I want and others that do what I want, but I don't understand.  I think I will do what I understand at the moment and us the put all call to for the first map and loop through, search and replace keys as needed to add the second.
 
Paul Peterson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried using the code below to merge the two HashMaps.  Both cases are failing to add the temp HashMap to the one defined earlier in the class.  Please let me know what I am missing.

As for the formatting issue, I was making that more difficult than I needed to, resolved that issue. 

 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if you don't see "inventory.putAll(temp)" doing anything to a certain HashMap, then either you're updating a different HashMap or else the "temp" HashMap is empty.
 
Paul Peterson
Ranch Hand
Posts: 104
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nevermind, another case of cranial flatulence.  It helps to call the method.  Both cases now working.
 
Paul Clapham
Sheriff
Posts: 22487
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Peterson wrote:It helps to call the method.


Yup, that could be another reason why you aren't seeing any effect... 
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!