File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

TreeMap question#2

 
Anthony Alexander
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Say I have two treemaps A and B defined as : <String, Integer>

eg.

Tree Map A
-----------
apple, 4
orange, 2

Tree Map B
-----------
banana, 3
orange, 1


How do I search for identical keys (orange in this example) and use the values for each key (eg. 2 + 1 = 3)?

Do I have to loop through every treemap looking at every key?
 
Keith Lynn
Ranch Hand
Posts: 2399
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the API for TreeMap, there is a method keySet which will return a Set containing the keys in the TreeMap.

In the Set interface there is a specification for the method retainAll which will produce the intersection of two sets.
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are only interested in keys that are identical to both TreeMaps then you can use the retainAll() method.

Assumiing you have two treeMaps that you want to find the identical keys for.
 
Anthony Alexander
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Yes. Good idea. that may work. thanks.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic