Guest User

Untitled

a guest
Oct 16th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. import java.util.LinkedHashMap;
  2. import java.util.LinkedList;
  3. import java.util.List;
  4. import java.util.Map;
  5. import java.util.Map.Entry;
  6.  
  7.  
  8. public class TestIterator {
  9.  
  10. public static void main(String[] args) {
  11. Map<String,Integer> map = new LinkedHashMap<String,Integer>();
  12. map.put("a", 1);
  13. map.put("c", 3);
  14. map.put("b", 2);
  15. map.put("d", 4);
  16.  
  17. List<Map<String,Integer>> list = getNewMapIterator(map);
  18. for (Map<String,Integer> m: list)
  19. {
  20. System.out.println(m);
  21. }
  22. }
  23.  
  24. private static List<Map<String, Integer>> getNewMapIterator(
  25. Map<String, Integer> map) {
  26. List<Map<String, Integer>> maplist = new LinkedList<Map<String,Integer>>();
  27.  
  28. if (map.size() == 1)
  29. {
  30. maplist.add(map);
  31. return maplist;
  32. }
  33.  
  34. for (Entry<String,Integer> one : map.entrySet()){
  35. Map<String,Integer> others = new LinkedHashMap<String,Integer>(map);
  36. others.remove(one.getKey());
  37. List<Map<String,Integer>> lst = getNewMapIterator(others);
  38. for (Map<String,Integer> m: lst)
  39. {
  40. Map<String,Integer> newmap = new LinkedHashMap<String,Integer>();
  41. newmap.put(one.getKey(), one.getValue());
  42. newmap.putAll(m);
  43. maplist.add(newmap);
  44. }
  45. }
  46. return maplist;
  47. }
  48. }
Add Comment
Please, Sign In to add comment