如何对此 Map<String, List<class>> 进行排序

我有这张地图,其中包含姓名、姓氏和其他个人信息,例如:


jhon:[doe];


ann:[devil]

我想要的是按字母顺序排列它们,但这种结构对我来说是新的。我怎样才能按字母顺序排列它们?这是我的代码


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

    }


catspeake
浏览 351回答 3
3回答

白板的微信

尝试:Map<String,&nbsp;List<MyClass>>&nbsp;sorted&nbsp;=&nbsp;map.entrySet() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.stream() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.sorted(Comparator.comparing(Map.Entry::getKey))&nbsp;//&nbsp;sort&nbsp;by&nbsp;key &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.collect(Collectors.toMap(Map.Entry::getKey,&nbsp;Map.Entry::getValue));
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java