aspose file tools*
The moose likes Performance and the fly likes entrySet and keySet in a Map Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Performance
Bookmark "entrySet and keySet in a Map" Watch "entrySet and keySet in a Map" New topic
Author

entrySet and keySet in a Map

praveen oruganti
Ranch Hand

Joined: Feb 05, 2007
Posts: 73
Whether there is any performance increase by using entrySet compared to keySet for a Hashmap.
When i run the findbugs plugins, it is showing use entrySet rather going for keySet in the performance category.

Anyone experience these kind of performance issues...


Regards,<br />Praveen Oruganti<br />Software Programmer
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15642
    
  15

The keyset is a core part of the HashMap data structure. The entryset has to be constructed by enumerating through the HashMap element by element, which is an extra layer of code. So yes, it's true that a keyset is slightly more efficient in most cases. However, how you use the data may nullify that advantage.

The final determinant, as always, is to measure. And, since there are so many things you can measure in a typical all, I'd only bother worrying about it if there were actual performance problems and the global measurements indicated that this was a bottleneck.


Customer surveys are for companies who didn't pay proper attention to begin with.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2969
    
    9
[Tim]: The keyset is a core part of the HashMap data structure. The entryset has to be constructed by enumerating through the HashMap element by element, which is an extra layer of code. So yes, it's true that a keyset is slightly more efficient in most cases.

I don't agree. An EntrySet is just a thin wrapper providing a view of objects that already exist inside the HashMap. Specifically, there is an array of HashMap.Entry obects, which implement Map.Entry. The EntrySet just provides access. There's no need to iterate through the whole map just to construct an EntrySet.

The reason FindBugs points out a possible performance issue is that, if you need to access every key and value, there is absolutely no reason to call get() on each key. Each key and value are already stored right next to each other anyway, internally, in a HashMap.Entry instance. So when you iterate an EntrySet, you're just accessing these elements directly. If instead you call get(), you're invoking hashCode() and causing the HashMap to do a lookup in its internal array. Admittedly this is an O(1) operation, but it's always going to take a bit longer than simply using Entry.getValue(), and there's no reason to make such a call when the key and value are stored next to each other in the first place.

I agree with the points about measuring performance, though, and in many, many applications, the difference will be insignificant. It's not likely to be worth your time to change your existing code for this. But I think it's perfectly sensible to get in the habit of iterating using entrySet() in the first place, for all new code.
[ October 31, 2008: Message edited by: Mike Simmons ]
Norman Larson
Greenhorn

Joined: Mar 05, 2009
Posts: 7
Depending on the size of your map, I've found you can also get a performance boost by converting the entrySet to an array via the toArray() function, and then iterating through that with a for loop (not for-each loop). But then again, I'm talking like a 3% total improvement, and it really depends on how often your function is called and how large the map is.
Jackie Li
Ranch Hand

Joined: Sep 12, 2010
Posts: 30
I think the findbug, or sonar, is referring the case, as instead of using keySet to get the key first, then using getValue from the key to get the value; using entry set can directly get the value intended.
Girish K Kumar
Greenhorn

Joined: Feb 23, 2011
Posts: 1
Both keySet & entrySet does a O(n) walk through on the the underlying map entries as we move forward. So in theory they all have same effect of traversal.

But if you are iterating through the map to get the key and values then using entrySet would be preferred option when compared to keySet. As in keySet we need an additional get(key) to retrieve the value. For HashMap() even though the operation of get is O(1), we need to consider the time taken for the computation of hash index and the time taken to resolve collisions.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: entrySet and keySet in a Map
 
Similar Threads
Am I guaranteed to get these in the same order?
retrieving keys with a map interator
Hash Map Query
retainAll for Map<String, String>
remove key from Map