1

I have the following Map:

Map<Long, List<Address>> map = new HashMap<Long, List<Address>>();

which is filled with pairs of keys and values. For example: key = student id and

value = list of Address. In Address object I have country name(String). I want to sort the total map by the country name. I have tried many ways but not getting the Idea. Any ideas? Below is my tried code.

private static Map<Long, List<Address>> sortByValue(Map<Long, List<Address>> unsortMap) {

    // Convert Map to List of Map
        List<Map.Entry<Long, List<Address>>> unSortedList =
                new ArrayList<Map.Entry<Long, List<Address>>>(unsortMap.entrySet());

    // sort the List
        Collections.sort(unSortedList, new Comparator<Map.Entry<Long, List<Address>>>() {
            public int compare(Map.Entry<Long, List<Address>> object1,
                               Map.Entry<Long, List<Address>> object2) {
            // sort by country name
                return ???;
            }
        });

     // Loop the sorted list and put it into a new insertion order Map LinkedHashMap
        Map<Long, List<Address>> sortedMap = new LinkedHashMap<Long, List<Address>>();

            for (Map.Entry<Long, List<Address>> entry : unSortedList) {
            sortedMap.put(entry.getKey(), entry.getValue());

        }
    return sortedMap;
    }
18
  • Which one out of the list do you want to compare? And what's stopping you? Commented May 17, 2017 at 7:21
  • I want sort the total map by countryName.But key should be id Commented May 17, 2017 at 7:22
  • I think little modify in domain object it will become simple.Create student object has id and address and sort that list and then populate in a map.Or treeMap you can pass your customize Comparator. Commented May 17, 2017 at 7:22
  • 1
    Why do you have a list of addresses? What if they contain different countries for a single student? Commented May 17, 2017 at 7:25
  • 1
    And why is the question posted by another user? Commented May 17, 2017 at 7:29

1 Answer 1

1

You can create a temporary TreeMap inside the method and store the reverse mappings (i.e. country -> keys) into it. Once done, you can iterate over it and fill the values in the result, e.g.:

public static Map<Long, List<Address>> sort(Map<Long, List<Address>> map){

    //Create temporary map, sorted by countries
    Map<String, List<Long>> countryMap = new TreeMap<>();

    map.entrySet().stream()
    .forEach(e -> {
        e.getValue()
        .stream()
        .map(a -> a.country)
        .forEach(c -> countryMap.computeIfAbsent(c, k -> new ArrayList<Long>()).add(e.getKey()));
    });

    //Iterate over treemap and populate the values in result
    Map<Long, List<Address>> sortedMap = new LinkedHashMap<>();
    countryMap.entrySet()
    .stream()
    .flatMap(e -> e.getValue().stream())
    .forEach(k -> sortedMap.put(k, map.get(k)));

    return sortedMap;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.