i have list of map of strings:
list<map<string, string>> list = new arraylist<map<string, string>>();
this gets populated following:
map<string, string> action1 = new linkedhashmap<>(); map.put("name", "createfirstname"); map.put("nextaction", "createlastname"); map<string, string> action2 = new linkedhashmap<>(); map.put("name", "createaddress"); map.put("nextaction", "createemail"); map<string, string> action3 = new linkedhashmap<>(); map.put("name", "createlastname"); map.put("nextaction", "createaddress"); map<string, string> action4 = new linkedhashmap<>(); map.put("name", "createemail"); list.add(action1); list.add(action2); list.add(action3); list.add(action4);
action4 doesn't have nextaction because last action, might easier give nextaction placeholder no next action?
question: how can sort list, actions in order? ie: nextaction of action, same name of next action in list.
although seems case of xy-problem, , list of maps not "nicely designed data model", , there representation "better" in many ways (although nobody can give recommendations "best" model be, long overall goal not known), task have @ hand, , here how solved:
first of all, have determine first element of sorted list. map has "name"
entry not appear "nextaction"
entry of other map.
after have first map, can add (sorted) list. then, determining next element boils down finding map "name"
same "nextaction"
of previous map. find these successors, can build map maps each "name"
entry map itself.
here basic implementation of sorting approach:
import java.util.arraylist; import java.util.collections; import java.util.linkedhashmap; import java.util.linkedhashset; import java.util.list; import java.util.map; import java.util.set; public class sortlistwithmaps { public static void main(string[] args) { list<map<string, string>> list = new arraylist<map<string, string>>(); map<string, string> action1 = new linkedhashmap<>(); action1.put("name", "createfirstname"); action1.put("nextaction", "createlastname"); map<string, string> action2 = new linkedhashmap<>(); action2.put("name", "createaddress"); action2.put("nextaction", "createemail"); map<string, string> action3 = new linkedhashmap<>(); action3.put("name", "createlastname"); action3.put("nextaction", "createaddress"); map<string, string> action4 = new linkedhashmap<>(); action4.put("name", "createemail"); list.add(action1); list.add(action2); list.add(action3); list.add(action4); // make bit more interesting... collections.shuffle(list); system.out.println("before sorting"); (map<string, string> map : list) { system.out.println(map); } list<map<string, string>> sortedlist = sort(list); system.out.println("after sorting"); (map<string, string> map : sortedlist) { system.out.println(map); } } private static list<map<string, string>> sort( list<map<string, string>> list) { // compute map "name" actual map map<string, map<string, string>> nametomap = new linkedhashmap<string, map<string,string>>(); (map<string, string> map : list) { string name = map.get("name"); nametomap.put(name, map); } // determine first element sorted list. that, // create set of names, , remove of them // appear "nextaction" of entry set<string> names = new linkedhashset<string>(nametomap.keyset()); (map<string, string> map : list) { string nextaction = map.get("nextaction"); names.remove(nextaction); } if (names.size() != 1) { system.out.println("multiple possible first elements: " + names); return null; } // insert elements, in sorted order, result list list<map<string, string>> result = new arraylist<map<string, string>>(); string currentname = names.iterator().next(); while (currentname != null) { map<string, string> element = nametomap.get(currentname); result.add(element); currentname = element.get("nextaction"); } return result; } }
Comments
Post a Comment