fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.util.Map.Entry;
  4.  
  5. class Main {
  6.  
  7. enum SomeEnum{
  8. ONE, TWO, THREE;
  9. }
  10.  
  11. public static void main(String[] args) {
  12. EnumMap<SomeEnum, Integer> map = new EnumMap<SomeEnum, Integer>(SomeEnum.class);
  13. map.put(SomeEnum.ONE, 1);
  14. map.put(SomeEnum.TWO, 2);
  15. map.put(SomeEnum.THREE, 3);
  16.  
  17. ArrayList<Entry<SomeEnum, Integer>> entryList = new ArrayList<Entry<SomeEnum, Integer>>();
  18.  
  19. for(Entry<SomeEnum, Integer> entry : map.entrySet()){
  20. System.out.println("Key is " + entry.getKey() + ", value is " + entry.getValue());
  21. //This prints the correct keys and values
  22.  
  23. entryList.add(entry);
  24. }
  25.  
  26. System.out.println("");
  27.  
  28. for(Entry<SomeEnum, Integer> entry:entryList){
  29. System.out.println("Key is " + entry.getKey() + ", value is " + entry.getValue());
  30. //This prints only the last entry each time
  31. }
  32. }
  33. }
Success #stdin #stdout 0.09s 212416KB
stdin
Standard input is empty
stdout
Key is ONE, value is 1
Key is TWO, value is 2
Key is THREE, value is 3

Key is THREE, value is 3
Key is THREE, value is 3
Key is THREE, value is 3