Java Iteration over a keySet
Asked Answered
T

7

17

I have the following Java code:

public void myMethod (final Map pFeatureGroupsFromPackage) {

   final Set<String> keys = pFeatureGroupsFromPackage.keySet();

   for (final String key : keys) {
           tmpList = (List<FeatureKey>) pFeatureGroupsFromPackage.get(key);
    // do whatever
   }
}

I am getting a warning from "findBugs" telling the following:

Method myMethod makes inefficient use of keySet iterator instead of entrySet iterator. The warning is done at the tmpListassignment.

I do not understand why this is inefficient. In fact the keyslist is computed only once. Any comment? Thanks.

Tasteless answered 25/3, 2011 at 9:54 Comment(0)
W
30

Instead of iterating over the keySet and calling get to get the corresponding value for each key, iterate over the entrySet:

final Set<Map.Entry<String, List<FeatureKey>>> entries = pFeatureGroupsFromPackage.entrySet();

for (Map.Entry<String, List<FeatureKey>> entry : entries) {
    String key = entry.getKey();
    List<FeatureKey> tmpList = entry.getValue();

    // do whatever
}

That way you don't have to do a lookup in the map for every key; you directly get the key and value in one go.

Also, declare your Map with type parameters:

public void myMethod (final Map<String, List<FeatureKey>> pFeatureGroupsFromPackage) {
    // ...
}
Wordsworth answered 25/3, 2011 at 9:57 Comment(1)
Hi Jesper. Thanks for your answer. BTW, there are two syntax errors (one missing '>' and it should be entry instead of enty) :-) +1 and accepted for youTasteless
P
5

you're getting all the keys and then you search for every key in the collection

a Map.EntrySet iteration would be much faster, a small example:

But you also should use generics...

Set entries = map.entrySet();
      Iterator entryIter = entries.iterator();
      System.out.println("The map contains the following associations:");
      while (entryIter.hasNext()) {
         Map.Entry entry = (Map.Entry)entryIter.next();
         Object key = entry.getKey();  // Get the key from the entry.
         Object value = entry.getValue();  // Get the value.
         System.out.println( "   (" + key + "," + value + ")" );
      }
Pueblo answered 25/3, 2011 at 9:56 Comment(0)
F
4

This could help you:

Map map = new HashMap();
Iterator entries = map.entrySet().iterator();
while (entries.hasNext()) {
    Map.Entry entry = (Map.Entry) entries.next();
    Integer key = (Integer)entry.getKey();
    Integer value = (Integer)entry.getValue();
    System.out.println("Key = " + key + ", Value = " + value);
}
Foundation answered 4/8, 2014 at 9:29 Comment(0)
A
1

It could be that you are querying the map twice:

  • first for the keys,
  • and second for the values

Using entryset iterator will iterate over the map once.

Alatea answered 25/3, 2011 at 9:58 Comment(0)
C
1

Sample code:

for (Map.Entry < Integer, List < FeatureKey >>> i: map.entrySet()) {
    System.out.println(i.getValue() + " " + i.getKey()));
}
Climatology answered 7/4, 2015 at 8:38 Comment(0)
P
0

Accessing the HashMap via keySet iterator is even faster than using the keySet iterator on the TreeMap.

Preoccupancy answered 25/3, 2011 at 9:59 Comment(0)
S
0

Hey Luixv, The reason using keysey iterator is less effective than entryset iteratot is that with the first option you still have to use the Map.get(key) lookeup which is avoided with the second option.

Safety answered 25/3, 2011 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.