• Map 集合按字母排序方法


    @Test
    public void testMapSort() {
    Map<String, String> map = new HashMap<>();
    map.put("bdc", "bbb");
    map.put("caf", "ccc");
    map.put("aef", "aaa");
    map.put("def", "ddd");
    System.out.println("排序前:" + map);

    //进行排序
    List<Map.Entry<String, String>> entries = new ArrayList<Map.Entry<String, String>>(map.entrySet());
    Collections.sort(entries, new Comparator<Map.Entry<String, String>>() {
    @Override
    public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
    return o1.getKey().compareTo(o2.getKey());
    }
    });
    System.out.println("排序后:" + entries);

    //将key,value拼接
    StringBuffer sb = new StringBuffer();
    entries.forEach(x -> sb.append(x.getKey()).append(x.getValue()));
    System.out.println("拼接后结果:" + sb.toString());
    }
    控制台输出结果:

    排序前:{bdc=bbb, aef=aaa, def=ddd, caf=ccc}

    排序后:[aef=aaa, bdc=bbb, caf=ccc, def=ddd]

    拼接后结果:aefaaabdcbbbcafcccdefddd
  • 相关阅读:
    Android网站
    vim里面搜索字符串
    ssd遇到的bug
    ssd训练自己的数据集
    slover层解读
    caffe LOG LOG_IF
    cuda输出
    css中合理的使用nth-child实现布局
    Linux VM环境配置
    怎样对Android设备进行网络抓包
  • 原文地址:https://www.cnblogs.com/ming-blogs/p/12071554.html
Copyright © 2020-2023  润新知