Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedHashMap;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- public class TestIterator {
- public static void main(String[] args) {
- Map<String,Integer> map = new LinkedHashMap<String,Integer>();
- map.put("a", 1);
- map.put("c", 3);
- map.put("b", 2);
- map.put("d", 4);
- List<Map<String,Integer>> list = getNewMapIterator(map);
- for (Map<String,Integer> m: list)
- {
- System.out.println(m);
- }
- }
- private static List<Map<String, Integer>> getNewMapIterator(
- Map<String, Integer> map) {
- List<Map<String, Integer>> maplist = new LinkedList<Map<String,Integer>>();
- if (map.size() == 1)
- {
- maplist.add(map);
- return maplist;
- }
- for (Entry<String,Integer> one : map.entrySet()){
- Map<String,Integer> others = new LinkedHashMap<String,Integer>(map);
- others.remove(one.getKey());
- List<Map<String,Integer>> lst = getNewMapIterator(others);
- for (Map<String,Integer> m: lst)
- {
- Map<String,Integer> newmap = new LinkedHashMap<String,Integer>();
- newmap.put(one.getKey(), one.getValue());
- newmap.putAll(m);
- maplist.add(newmap);
- }
- }
- return maplist;
- }
- }
Add Comment
Please, Sign In to add comment