我有这张地图,其中包含姓名、姓氏和其他个人信息,例如: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(); }
3 回答
白板的微信
TA贡献1883条经验 获得超3个赞
尝试:
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));
添加回答
举报
0/150
提交
取消