进行排序

public static void main(String[] args) {
        Map<String, String> map = new TreeMap<String, String>();
        map.put("d", "ddddd");
        map.put("c", "ccccc");
 
        List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, String>>() {
            public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
                return o1.getKey().compareTo(o2.getKey());
            }
        });
        String string = String.valueOf(list.get(0)) + String.valueOf(list.get(1));
        System.out.println(string);
 
    }

replace with lambda

Collections.sort(list, (o1, o2) -> o1.getKey().compareTo(o2.getKey()));

replace with Comparator.comparing

Collections.sort(list, Comparator.comparing(Map.Entry::getKey));

 

结束