Java之HashMap之排序方法大全

新年 Java经验 发布时间:2024-02-17 21:03:16 阅读数:17793 1
下文笔者分享所有的HashMap排序方法,如下所示

Hash Map排序实现思路

 使用stream 排序
 转TreeMap自定义排序
例:HashMap排序的示例
package com.java265.a;
 
import java.util.*;
 
public class Demo {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("ad", "dd");
        map.put("bc", "ee");
        map.put("cb", "ff");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
        System.out.println();
 
        Map<String, String> LinkedHashMap = new LinkedHashMap<>();
        // 默认按照升序排列
        map.entrySet().stream().sorted(Map.Entry.comparingByKey())
                .forEach(o -> linkedHashMap.put(o.getKey(), o.getValue()));
        for (Map.Entry<String, String> entry : linkedHashMap.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
        System.out.println();
 
        Map<String, String> linkedHashMap1 = new LinkedHashMap<>();
        // 自定义排序(降序)
        map.entrySet().stream().sorted(Map.Entry.comparingByKey(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o2.compareTo(o1);
            }
        })).forEach(o -> linkedHashMap1.put(o.getKey(), o.getValue()));
        for (Map.Entry<String, String> entry : linkedHashMap1.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }
}

----运行以上代码,将输出以下信息----

bc:ee
ad:dd
cb:ff
 
ad:dd
bc:ee
cb:ff
 
cb:ff
bc:ee
ad:dd

HashMap转TreeMap自定义排序(按key升序/降序)

package com.java265.a;
 
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
 
public class Demo {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("ad", "dd");
        map.put("bc", "ee");
        map.put("cb", "ff");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
        System.out.println();
        // 默认按照升序排序
        Map<String, String> map1 = new TreeMap<>();
        map.forEach(map1::put);
        for (Map.Entry<String, String> entry : map1.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
        System.out.println();
 
        // 自定义排序(降序)
        Map<String, String> map2 = new TreeMap<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o2.compareTo(o1);
            }
        });
        map.forEach(map2::put);
        for (Map.Entry<String, String> entry : map2.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }
}

----运行以上代码,将输出以下信息-----

bc:ee
ad:dd
cb:ff
 
ad:dd
bc:ee
cb:ff
 
cb:ff
bc:ee
ad:dd

按value排序

使用stream进行排序(按value升序/降序)
package com.java265.a;
 
import java.util.*;
 
public class Demo {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("ad", "dd");
        map.put("bc", "ee");
        map.put("cb", "ff");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
        System.out.println();
 
        Map<String, String> linkedHashMap = new LinkedHashMap<>();
        // 默认按照升序排列
        map.entrySet().stream().sorted(Map.Entry.comparingByValue())
                .forEach(o -> linkedHashMap.put(o.getKey(), o.getValue()));
        for (Map.Entry<String, String> entry : linkedHashMap.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
        System.out.println();
 
        Map<String, String> linkedHashMap1 = new LinkedHashMap<>();
        // 自定义排序(降序)
        map.entrySet().stream().sorted(Map.Entry.comparingByValue(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o2.compareTo(o1);
            }
        })).forEach(o -> linkedHashMap1.put(o.getKey(), o.getValue()));
        for (Map.Entry<String, String> entry : linkedHashMap1.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }
}

----运行以上代码,将输出以下信息-----

bc:ee
ad:dd
cb:ff
 
ad:dd
bc:ee
cb:ff
 
cb:ff
bc:ee
ad:dd

使用list进行排序(按value升序/降序)

原理:将待排序Map中的所有元素置于一个列表中,
 接着使用Collections的一个静态方法 
  sort(List<T> list, Comparator<? super T> c) 
  来排序列表,同样是用比较器定义比较规则。
  排序后的列表中的元素再依次装入Map,
  为了肯定的保证Map中元素与排序后的List中的元素的顺序一致,
  使用LinkedHashMap数据类型
package com.java265.a;
 
import java.util.*;
 
public class Demo {
    public static void main(String[] args) {
        Map<String, String> map = new HashMap<>();
        map.put("ad", "dd");
        map.put("bc", "ee");
        map.put("cb", "ff");
        for (Map.Entry<String, String> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
        System.out.println();
 
        Map<String, String> sortedMap = new LinkedHashMap<>();
        List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(
                map.entrySet());
        Collections.sort(entryList, new Comparator<Map.Entry<String, String>>() {
            @Override
            public int compare(Map.Entry<String, String> me1, Map.Entry<String, String> me2) {
                return me1.getValue().compareTo(me2.getValue());
            }
        });
        for (Map.Entry<String, String> stringStringEntry : entryList) {
            sortedMap.put(stringStringEntry.getKey(), stringStringEntry.getValue());
        }
 
        for (Map.Entry<String, String> entry : sortedMap.entrySet()) {
            System.out.println(entry.getKey() + ":" + entry.getValue());
        }
    }
}


----运行以上代码,将输出以下信息----
bc:ee
ad:dd
cb:ff
 
ad:dd
bc:ee
cb:ff

按插入顺序存放

HashMap不按插入顺序存放
package com.java265.a;
 
import java.util.*;
 
public class Demo{
    public static List arrayList = new ArrayList();
    public static void main(String[] args) {
        Map<String, String> hashMap = new HashMap<String, String>();
        hashMap.put("name1", "josan1");
        hashMap.put("name2", "josan2");
        hashMap.put("name3", "josan3");
        
        Set<Map.Entry<String, String>> set = hashMap.entrySet();
        Iterator<Map.Entry<String, String>> iterator = set.iterator();
        while(iterator.hasNext()) {
            Map.Entry entry = iterator.next();
            String key = (String) entry.getKey();
            String value = (String) entry.getValue();
            System.out.println("key:" + key + ", value:" + value);
        }
    }
}

执行结果(未按照插入顺序输出)

key:name3, value:josan3
key:name2, value:josan2
key:name1, value:josan1

LinkedHashMap会按照插入顺序存放

package com.java265.a;
 
import java.util.*;
 
public class Demo{
    public static List arrayList = new ArrayList();
    public static void main(String[] args) {
        Map<String, String> hashMap = new LinkedHashMap<String, String>();
        hashMap.put("name1", "josan1");
        hashMap.put("name2", "josan2");
        hashMap.put("name3", "josan3");
 
        Set<Map.Entry<String, String>> set = hashMap.entrySet();
        Iterator<Map.Entry<String, String>> iterator = set.iterator();
        while(iterator.hasNext()) {
            Map.Entry entry = iterator.next();
            String key = (String) entry.getKey();
            String value = (String) entry.getValue();
            System.out.println("key:" + key + ", value:" + value);
        }
    }
}

执行结果(按照插入顺序输出)

key:name1, value:josan1
key:name2, value:josan2
key:name3, value:josan3 
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202402/17081764197968.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者