How can I get the sorted arrayList in HashMap? [closed] Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How can I get the sorted arrayList in HashMap? [closed] without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I have tried the following java code. But I can’t get the correct output.

public static void main(String[] args) throws IOException 

    {
ArrayList<HashMap<ArrayList<String>,Integer>> result=new ArrayList<>();     

    List<ArrayList<String>> list=new ArrayList<>();
        list.add(new ArrayList<>(Arrays.asList("a")));
        list.add(new ArrayList<>(Arrays.asList("a,b,c")));
        list.add(new ArrayList<>(Arrays.asList("a,c")));
        list.add(new ArrayList<>(Arrays.asList("c")));
        list.add(new ArrayList<>(Arrays.asList("b,d")));
        list.add(new ArrayList<>(Arrays.asList("b")));

        ArrayList<Integer> value=new ArrayList<Integer>();
        value.add(1);
        value.add(5);
        value.add(3);
        value.add(4);
        value.add(2);
        value.add(1);

        HashMap<ArrayList<String>,Integer> map=new HashMap<ArrayList<String>,Integer>();
        for(int i=0;i<list.size();i++)
        {
            map.put(list.get(i),value.get(i));
        }
        result.add(map);
        System.out.println(result);//output : [{[a]=1, [a,b,c]=5, [b]=1, [c]=4, [a,c]=3, [b,d]=2}]
    }

I got the output:

[{[a]=1, [a,b,c]=5, [b]=1, [c]=4, [a,c]=3, [b,d]=2}]

But, I want to get the sorted order by their word count such as:

[{[a]=1,[c]=4,[b]=1},{[a,c]=3, [b,d]=2},{ [a,b,c]=5}]

Answer

Ok, so here’s what I think you need:

public static void main(String[] args) {
    List<Map<List<String>, Integer>> mapList = new ArrayList<>();

    put(mapList, Arrays.asList("a"),           1);
    put(mapList, Arrays.asList("a", "b", "c"), 5);
    put(mapList, Arrays.asList("a", "c"),      3);
    put(mapList, Arrays.asList("c"),           4);
    put(mapList, Arrays.asList("b", "d"),      2);
    put(mapList, Arrays.asList("b"),           1);

    System.out.println(mapList); // Output: [{[a]=1, [b]=1, [c]=4}, {[a, c]=3, [b, d]=2}, {[a, b, c]=5}]
}

private static void put(List<Map<List<String>, Integer>> mapList, List<String> words, Integer number) {
    assert !words.isEmpty() : "Must be at least one word in list";

    // First, make sure our list of maps is long enough for the word list we're adding
    while(mapList.size() < words.size()) {
        mapList.add(new HashMap<>());
    }

    // Now, add the words and number to the appropriate map
    mapList.get(words.size() - 1).put(words, number);
}

Alternatively, we can construct an initial Map containing everything, then partition it by word count and sort the resulting collection. This approach means that if you don’t have any word lists of a particular length, then no empty Map for holding such word lists will be added to the result (in the above solution, it would be). The code for this solution is a bit harder to understand – you may need to do sume reading around:

public static void main(String[] args) {
    Map<List<String>, Integer> initial = new HashMap<>();

    initial.put(Arrays.asList("a"),           1);
    initial.put(Arrays.asList("a", "b", "c"), 5);
    initial.put(Arrays.asList("a", "c"),      3);
    initial.put(Arrays.asList("c"),           4);
    initial.put(Arrays.asList("b", "d"),      2);
    initial.put(Arrays.asList("b"),           1);
    initial.put(Arrays.asList("v", "w", "x", "y", "z"), 99);

    List<Map<List<String>, Integer>> result = new ArrayList<>(
        initial.entrySet() // Get an unordered collection of entries
            .stream()
            .collect(Collectors.groupingBy(
                e -> e.getKey().size(), // Group by word list size...
                TreeMap::new, // ...into a sorted map...
                Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue) // ... collecting all values with the same word count into individual Maps
            ))
            .values() // Construct our ArrayList using the ordered collection of map values
    );

    System.out.println(result); // Output: [{[a]=1, [b]=1, [c]=4}, {[a, c]=3, [b, d]=2}, {[a, b, c]=5}, {[v, w, x, y, z]=99}]
    // Note that the above result doesn't contain an empty Map ('{}') for 4-word lists
}
We are here to answer your question about How can I get the sorted arrayList in HashMap? [closed] - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji