简体   繁体   English

如何对地图进行排序 <String, List<class> &gt;

[英]How to sort this Map<String, List<class>>

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();
    }

I think you should checkout another interface: the SortedMap ( https://docs.oracle.com/javase/8/docs/api/java/util/SortedMap.html ) 我认为您应该签出另一个接口: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);

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

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 ie the name LinkedHashMap将保留将数据添加到Map的顺序, TreeMap将根据密钥对数据进行排序,在您的情况下为String即名称

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

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM