File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Why use Map.entrySet() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why use Map.entrySet()" Watch "Why use Map.entrySet()" New topic
Author

Why use Map.entrySet()

Zhixiong Pan
Ranch Hand

Joined: Jan 25, 2006
Posts: 239
Hi ranchers,
In what cases, will Map.entrySet() be used? Thanks.


SCJP 1.4 SCJD
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6657
    
    5

I have not personally used this myself. Each entry in the Map is represented by a Map.Entry, and the entry set can be used to iterate through the entries. Useful if you need to access each entry as a whole.


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36482
    
  16
I think I have used it and John Meyers is correct. I am writing from memory, but it goes something like this:-
It was the only way I could find of gaining access to the "V" entries in a HashMap in order without knowing the "K" values. Of course, you can get access to the "K" values using getKey() instead of getValue().

I think that is how it is supposed to work.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
If you just need keys, use keySet(). If you just need values, use values(). If you're going to use keys and values in your subsequent code, then you're best off using entrySet().

I frequently see people do this without entrySet(), and it usually looks something like this:

This works, but it's making the JVM do extra work for no good reason. Every time you call get() you're making the JVM spend time doing a hashcode lookup, or navigating a tree and evaluating a comparator. These operations may be fast, or not, but why do them if you don't have to? A Map.Entry gives you both key and value, together, in the most efficient manner possible.

Under JDK 5 and later it's a little nicer:


"I'm not back." - Bill Harding, Twister
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18116
    
  39

Originally posted by Jim Yingst:

I frequently see people do this without entrySet(), and it usually looks something like this:



I do this a *lot*. I don't really know why -- maybe it has to do with muscle memory. It does seem to be a habit that I can't seem to break.

So, I guess my response to "Why use Map.entrySet()" is ... I don't, but I should...

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60053
    
  65

I must be ahead of the curve . I use it all the time!


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mr. C Lamont Gilbert
Ranch Hand

Joined: Oct 05, 2001
Posts: 1170

Whenever I am getting all the data out of the Map I use entrySet. I do use it a lot. Especially when copying the Map, or converting it into a different format. Most recently I created a database dump to xml and I had to get all the contents of a Map. I used entryset.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why use Map.entrySet()
 
Similar Threads
Generics -really very hard question
Without Key can we retrieve particular value in hashmap
Compiler error in case of advanced for-each loop of Map.Entry
How get haspmap in inserted order?
Iterator in Generics