Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

TreeMap question#2

 
Greenhorn
Posts: 15
  • 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?
 
Ranch Hand
Posts: 2410
  • 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.
 
Ranch Hand
Posts: 1296
  • 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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Yes. Good idea. that may work. thanks.
 
30 seconds to difuse a loaf of bread ... here, use this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic