This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of Machine Learning with R: Expert techniques for predictive modeling and have Brett Lantz on-line!
See this thread for details.
Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Common and unique values in Maps

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey. I have two maps : Map<String, int> m1 = new HashMap<>() and Map<String, int> m2 = new HashMap<>();
and these have values respectively as m1 : { a=20, b=40, c=40} and m2: { a=19, x= 30, y=80}
And I want to go through each map and its values (they have "a" a common and I want both values of "a" summed up) and print both common and unique values  and store  to further use them. How do I do that?
 
Saloon Keeper
Posts: 10675
228
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

You can iterate through the entries of m2 using m2.forEach() and then call m1.merge() for each element of m2.

If you don't want to modify the contents of m1, make a copy named result of it first and then call result.merge() in the loop.
 
Marshal
Posts: 65821
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rish Dave wrote:. . . Map<String, int> . . .

Surely you mean Map<String, Integer>; you can't use int as a generic type.

And welcome to the Ranch (again)
 
Rish Dave
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am unable to iterate and add up vales for same key for both maps. I need help.
 
Campbell Ritchie
Marshal
Posts: 65821
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain a bit more. What are you going to do about "a"? How are you getting the “V” associated with "a" from the two maps? What about "d"? What if one map contains "e" as a key but not the other?
You may work out the whole solution as you do that; this is sometimes called “rubber‑duck programming”.
 
Rish Dave
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to merge both maps and if a key has different values in both maps, those values should be added for the particular key from different maps. The merged map should have unique keys and its values along with added values for same key.
 
Sheriff
Posts: 21805
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Rish Dave wrote:. . . Map<String, int> . . .

Surely you mean Map<String, Integer>; you can't use int as a generic type.


Just wait a few years, and that statement will be false. I quote from a reply I got to a patch I submitted for OpenJDK:

Brian Goetz wrote:We are currently trying to avoid adding anything to OptionalInt/Long/Double, because our current plan is to replace these with a specialized Optional<int> once Valhalla delivers.

 
Marshal
Posts: 14060
234
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rish Dave wrote:I need to merge both maps and if a key has different values in both maps, those values should be added for the particular key from different maps. The merged map should have unique keys and its values along with added values for same key.



Tips:
- Map has a keySet() method: https://docs.oracle.com/javase/8/docs/api/java/util/Map.html#keySet--
- Check out the Set API: https://docs.oracle.com/javase/8/docs/api/java/util/Set.html

By definition, elements in a Set are unique, no matter how many times you try to add the same element. java.util.Set also has methods that allow you to perform the equivalent of ∩ (intersection) and ∪ (union) set operations.
 
Rancher
Posts: 3371
31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Map also has a merge() method (since Java 8) that handles this quite nicely.  You need to first be comfortable with lambdas, but if you are, it's the way to go...
 
Campbell Ritchie
Marshal
Posts: 65821
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:. . . Just wait a few years, and that statement will be false. . . .

The sooner, the better.
 
Bartender
Posts: 3519
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another simple way is to make a stream of both map's entrysets, and the result is easily obtained. For instance:

In mapresult, a key is unique to one of the maps if its value has only one element.
 
I'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!