Java实现HashMap排序方法的示例详解

2022-11-13 08:11:12 示例 排序 详解

简介

本文用示例介绍HashMap排序的方法。

排序已有数据

按key排序

使用stream进行排序(按key升序/降序)

package org.example.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 org.example.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 org.example.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 org.example.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 org.example.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 org.example.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 

以上就是Java实现HashMap排序方法的示例详解的详细内容,更多关于Java HashMap排序的资料请关注其它相关文章!

相关文章