What do you mean by that?
//This prints only the last entry each time
Using this piece of code (Java 7):
import java.util.ArrayList; import java.util.EnumMap; import java.util.List; import java.util.Map; public class ForeachLoopWithFinal { enum MyEnum { ONE("one"), TWO("two"), THREE("three"); private String name; private MyEnum(String name) { this.name = name; } @Override public String toString() { return name; } } public static void main(String[] args) { List<Map.Entry<MyEnum, Integer>> entryList = new ArrayList<>(); Map<MyEnum, Integer> map = new EnumMap<>(MyEnum.class); map.put(MyEnum.ONE, 1); map.put(MyEnum.TWO, 2); map.put(MyEnum.THREE, 3); for (final Map.Entry<MyEnum, Integer> entry : map.entrySet()) { System.out.printf("Key is %s, value is %s%n", entry.getKey(), entry.getValue()); entryList.add(entry); } for (Map.Entry<MyEnum, Integer> entry : entryList) { System.out.printf("Key is %s, value is %s%n", entry.getKey(), entry.getValue()); } }
}
I get exactly this conclusion. Thus, the same number of entries are printed for the first and second time.
Key is one, value is 1 Key is two, value is 2 Key is three, value is 3 Key is one, value is 1 Key is two, value is 2 Key is three, value is 3
Using your example, I get the same thing.
Key is ONE, value is 1 Key is TWO, value is 2 Key is THREE, value is 3 Key is ONE, value is 1 Key is TWO, value is 2 Key is THREE, value is 3