How to sort this Map<String, List<class>>

sort map c
java sort map by key
sort list of map in java 8
map sort javascript
java sort map by value descending
guava sort map
java 8 sort map by custom comparator
map descending order java

I have this Map, which contains names, last names, and other personal information , for exmaple:

jhon:[doe];

ann:[devil]

What I want is to order them alphabetically, but this kind of structure is new to me. How can I order them alphabetically? Here my code

 private String uid;
    private String username;
    private String fullName;
    private String name;
    private String lastname;
    private String email;
    private String phone;
    private Map<String, List<PorticoProfile>> profiles;

    public Map<String, List<PorticoProfile>> getProfiles() {
        if (profiles == null) {
            profiles = new LinkedHashMap<>();
        }
        return profiles;
    }

    public void setProfiles(Map<String, List<PorticoProfile>> profiles) {
        this.profiles = profiles;
    }

    public void setFullName(String fullName) {
        this.fullName = fullName;
        if (fullName.contains(" ")){
            String[] nameParts = fullName.split(" ");
            this.name = nameParts[0];
            this.lastname = nameParts[1];
        }
    }

    @Override
    public String toString(){
        final String BREAK = System.getProperty("line.separator");
        StringBuilder sb = new StringBuilder();
        sb.append("uid: ").append(this.uid).append(BREAK);
        sb.append("username: ").append(this.username).append(BREAK);
        sb.append("fullname: ").append(this.fullName).append(BREAK);
        sb.append("email: ").append(this.email).append(BREAK);
        sb.append("phone: ").append(this.phone).append(BREAK);
        if (this.getProfiles().size() > 0){
            sb.append("profiles: ").append(this.profiles.keySet().stream().collect(Collectors.joining(", "))).append(BREAK);
        }
        return sb.toString();
    }

Use TreeMap - https://docs.oracle.com/javase/8/docs/api/java/util/TreeMap.html

LinkedHashMap will preserve the order in which you add data to the Map, TreeMap will keep it sorted based on the key, which in your case is String i.e. the name

Map<String, List<PorticoProfile>> profiles = new TreeMap<>();;

Sort a Map<Key, Value> by values, Here's a generic-friendly version: public class MapUtil { public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(Map<K, V> map)  In this post, we will discuss how to sort a map by values in C++.. We know that std::map container sorts its elements by keys by default and not by values. This post provides an overview of some of the available alternatives to accomplish this. The idea is to convert the std::map into a std::vector of key-value pairs.

I think you should checkout another interface: the SortedMap (https://docs.oracle.com/javase/8/docs/api/java/util/SortedMap.html)

    Map<String, Object> m = new HashMap<>();
    m.put("john", "doe");
    m.put("ann", "devil");

    SortedMap<String, Object> s = new TreeMap<>(m);
    s.entrySet().forEach(System.out::println);

How to Sort a Map by Value in Java 8+, Here, I am using LinkedHashMap to store the sorted result to preserve the order of the elements in the resulting map. The advantages of this  Based on @devinmoore code, a map sorting methods using generics and supporting both ascending and descending ordering. /** * Sort a map by it's keys in ascending order. * * @return new instance of {@link LinkedHashMap} contained sorted entries of supplied map.

Try:

Map<String, List<MyClass>> sorted = map.entrySet()
        .stream()
        .sorted(Comparator.comparing(Map.Entry::getKey)) // sort by key
        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

Java sort Map by values (ascending and descending orders), Alternatively, you can pass a custom Comparator to use in sorting. This can be used to sort the map in reverse order. In ascending order. Map<String,  A map is an unordered collection of key-value pairs.; If you need a stable iteration order, you must maintain a separate data structure. This example uses a sorted slice of keys to print a map[string]int in key order.

How to sort a Map by keys in Java 8 - Example Tutorial, Then, how can you sort a Map which doesn't support order? Well, you can't and that's why you only sort entries of HashMap but you don't store  Simple quick to use examples to sort a Map by key, using TreeMap and Stream APIs, in ascending and descending (reverse) orders. Sort Map by Key using TreeMap In ascending order. By default, all key-value pairs in TreeMap are sorted in their natural order. So all you need to do is add all unsorted key-value pairs in TreeMap.

Java Program to Sort a Map By Values, Example: Sort a map by values. import java.util.*; public class SortMap { public static void main(String[] args) { LinkedHashMap<String, String> capitals = new  Since there is no method in Java Collection API to sort Map, we need to use Collections.sort() method which accepts a List. This involves creating a temporary ArrayList with entries for sorting purpose and then again copying entries from sorted ArrayList to a new LinkedHashMap to keep them in sorted order.

Java – Sort Map By Value, In Java, we can use the TreeMap class to sort a map by its keys. This class is very handy to use. However, sometimes we need to sort a map by its values. In java 8, Map.Entry class has static method comparingByValue () to help you in sorting by values. This method returns a Comparator that compares Map.Entry in natural order on values. Alternatively, you can pass a custom Comparator to use in sorting. This can be used to sort the map in reverse order. In ascending order.

Comments
  • by using TreeMap you can get sorted values based on Key in Map.
  • @OHGODSPIDERS according to both , due to sometimes we have to do the search by name andothers by last name
  • @Amogh it can´t be done with the code I already have?
  • @Tony, check answer given by Friso. You don't have to change anything. In toString() you only have to create TreeMap using profiles so that its sorted then go for append operation.