This week's book giveaway is in the Java in General forum.
We're giving away four copies of Event Streams in Action and have Alexander Dean & Valentin Crettaz on-line!
See this thread for details.
Win a copy of Event Streams in Action this week in the Java in General 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 ...
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Confused about TreeMap, entrySet, and Iterator

Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think I understand the subject topics very well. I have a TreeMap that seems to work for storing a retrieving data. The value is a Drill object, and the key is a String of the object. I have a bunch of junit tests that seem to indicate it works. The show method in the code produces the right number of output lines. I know I don't understand it; I just copied an example from somewhere. The method isRealDrill is a copy of the show method that tries to look at each at each Drill in the map. When I run the tests for isRealDrill,
they all fail at the line with the cast to Drill, from the object that is returned by the Iterator. What exactly is returned. the key, the value, both (in what format?), or what? Before anyone mentions it, I know I can solve the problem faster using the getDrill method. But why does the cast not work? I think the same problem will show up when I try to make the show method produce prettier output.

The problem is line 29. Ignore line 30, a leftover from a failed effort.


Posts: 4179
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can follow the javadoc link that was created when you typed TreeMap in your post. You will see that the entrySet method returns a set of Map.Entry<K,V>. Which means, since you have a TreeMap<String,Drill> when you get the entrySet you get a Set<Map.Entry<String,Drill>> and from that you get an Iterator<Map.Entry<String,Drill>> and each item in the Iterator is of type Map.Entry<String,Drill>. Then to get the key from the value you use curentDrill.getKey() and to get the drill you get currentDrill.getValue()
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!