ANucool Mittal
ANucool Mittal

Reputation: 205

How to sort map or which traversal approach i need to follow?

i just want to get a Sorted map my code is like :

public class SubString {public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("string2");
list.add("STR_str2");
list.add("STR_str3");
getSequesce("STR_str1", list);
List<String> list1 = new ArrayList<>();
list1.add("STR_xyz");
list1.add("STR_ABC");
getSequesce("STR_str2", list1);
List<String> list3 = new ArrayList<>();
list3.add("Anukul");
list3.add("mittal");
getSequesce("STR_str3", list3);
List<String> list4 = new ArrayList<>();
list4.add("Test");
list4.add("STR_XYZ");
getSequesce("STR_ABC", list4);
List<String> list5 = new ArrayList<>();
list5.add("val");
list5.add("var");
getSequesce("STR_XYZ", list5);
List<String> list6 = new ArrayList<>();
list6.add("val6");
list6.add("valtest");
getSequesce("STR_free", list6);
List<String> list7 = new ArrayList<>();
list7.add("val6");
list7.add("STR_free");
getSequesce("STR_7", list7);

}private static void getSequesce(String string, List<String> list) {
Map<String, List<String>> map = new HashMap<>();
Map<String, List<String>> sortedMap = new TreeMap<>();
map.put(string, list);

for (Map.Entry<String, List<String> > itrMap : map.entrySet() ) {

}
}}

In my fist call of getSequence method i have put a string "STR_str1" and a list. i just want to add this into a map where key is STR_str1 and its value is list. but my problem is i have to put STR_str2 ,and STR_str3 as key in map before STR_str1. similarly i have to put STR_ABC and STR_XYZ before STR_str2. i just want a sortedMap from function getSequesce so that i get output like

STR_free,list6

"STR_7", list7

STR_XYZ,list5

STR_ABC,list4

STR_str3,list3

"STR_str2", list1

"STR_str1", list

if value of list start with STR_ then this STR_ must already avail in map.

position of "STR_free", list6 "STR_XYZ", list5 "STR_str3", list3 can be anywhere because they don't contain any dependency.

please help me to suggest what approach i can follow. i have data that will not create cyclic problem. Thanks.

Upvotes: 0

Views: 65

Answers (2)

ANucool Mittal
ANucool Mittal

Reputation: 205

Thank you for help guys code i was looking for is

private static Map<String, List<String>> map = new LinkedHashMap<String, List<String>>();
private static List<String> nonDependentList = new ArrayList<String>();
private static Map<String, List<String>> dependentMap = new ConcurrentHashMap<String, List<String>>();

public static void main(String[] args) {

    List<String> list1 = new ArrayList<>();
    list1.add("string2");
    list1.add("STR_str2");
    list1.add("STR_str3");
    addRecord("STR_str1", list1);
    List<String> list2 = new ArrayList<>();
    list2.add("STR_xyz");
    list2.add("STR_ABC");
    addRecord("STR_str2", list2);
    List<String> list3 = new ArrayList<>();
    list3.add("Anukul");
    list3.add("mittal");
    addRecord("STR_str3", list3);
    List<String> list4 = new ArrayList<>();
    list4.add("Test");
    list4.add("STR_XYZ");
    addRecord("STR_ABC", list4);
    List<String> list5 = new ArrayList<>();
    list5.add("val");
    list5.add("var");
    addRecord("STR_XYZ", list5);
    List<String> list6 = new ArrayList<>();
    list6.add("val6");
    list6.add("valtest");
    addRecord("STR_free", list6);
    List<String> list7 = new ArrayList<>();
    list7.add("val6");
    list7.add("STR_free");
    addRecord("STR_7", list7);

    Map<String, List<String>> processedMap = proceedAndFetchRecords();
    System.out.println("Final result");
    for (Entry<String, List<String>> entry : processedMap.entrySet()) {
        System.out.println("Key : " + entry.getKey() + " || Values : " + entry.getValue());
    }
}

private static Map<String, List<String>> proceedAndFetchRecords() {
    for (Entry<String, List<String>> entry : map.entrySet()) {
        boolean flag = isNotDependent(entry.getValue());
        // System.out.println("Key : " + entry.getKey() + " isNotDependent :
        // " + flag);
        if (flag) {
            nonDependentList.add(entry.getKey());
        } else {
            List<String> list = getOnlyDependentList(entry.getValue());
            dependentMap.put(entry.getKey(), list);
        }
    }
    // showDependentMap();
    refreshDependentMap();

    // Final result
    Map<String, List<String>> processedMap = addResultsInMap();
    return processedMap;
}

private static void refreshDependentMap() {
    for (Entry<String, List<String>> entry : dependentMap.entrySet()) {
        List<String> list = entry.getValue();
        list.removeAll(nonDependentList);

        boolean flag = isNotDependent(list);

        if (flag) {
            nonDependentList.add(entry.getKey());
            dependentMap.remove(entry.getKey());
            refreshDependentMap();
        } else {
            continue;
        }
    }

}

private static Map<String, List<String>> addResultsInMap() {
    Map<String, List<String>> processedMap = new LinkedHashMap<String, List<String>>();

    for (String key : nonDependentList) {
        processedMap.put(key, map.get(key));
    }

    for (Entry<String, List<String>> entry : dependentMap.entrySet()) {
        processedMap.put(entry.getKey(), map.get(entry.getKey()));
    }
    return processedMap;
}

private static boolean isNotDependent(List<String> list) {
    int count = 0;
    for (String string : list) {
        if (!string.startsWith("STR_")) {
            ++count;
        }
    }
    if (list.size() == count) {
        return true;
    }
    return false;
}

private static void addRecord(String structureName, List<String> list) {
    map.put(structureName, list);
}

private static List<String> getOnlyDependentList(List<String> list) {
    List<String> list1 = new ArrayList<>();
    for (String string : list) {
        if (string.startsWith("STR_")) {
            list1.add(string);
        }
    }
    return list1;
}}

Upvotes: 0

Michael Gantman
Michael Gantman

Reputation: 7790

Map by definition is not sorted (The same as set) so the traversal order is not guaranteed. However there is a interface SortedMap and its implementations (Such as TreeMap). In this case your keys must implement equals() and hashcode() in a meaningful way or implement Comparable interface. In your case you use Strings as keys and String implements comparable. So you can use SortedMap

Upvotes: 1

Related Questions