Java中对HashMap进行排序

文章目录

  • 前言
  • 一、通过TreeMap
  • 二、通过TreeSet
  • 三、通过ArrayList
  • 四、通过Collections.sort()
  • 全部代码


前言

提示:这里可以添加本文要记录的大概内容:


首先创建Person类和创建map:

class Person{
    private int age;
    private String name;
    public Person(int age, String name) {
        this.age=age;
        this.name=name;
    }
    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

public class Main{
    static HashMap<Integer,Person> map=new HashMap<>();
    public static void constructmap(){
        map.put(1600,new Person(16,"战肖"));
        map.put(1000,new Person(12,"张三"));
        map.put(1500,new Person(15,"李莹"));
        map.put(1200,new Person(13,"李四"));
        map.put(1300,new Person(14,"王五"));
    }
}

一、通过TreeMap

由于TreeMap的内部是有序的,它默认就是按照key值进行排序的,所以可以将HashMap中的元素放到TreeMap中来实现按照key值排序:

  • 使用构造函数创建
    public static void test01(){
        TreeMap<Integer,Person> treeMap=new TreeMap<>(map);
        System.out.println(treeMap);
    }
  • 使用putAll()方法
    public static void test02(){
        TreeMap<Integer,Person> treeMap=new TreeMap<>();
        treeMap.putAll(map);
        System.out.println(treeMap);
    }

二、通过TreeSet

如果在元素不重合时需要实现排序可以考虑使用TreeSet();

  • 仅对key进行排序
public static void test03(){
        TreeSet<Integer> treeSet=new TreeSet<>(map.keySet());
        System.out.println(treeSet);
}
  • 仅对value进行排序(会抛出error的,需对Person类的Comparator方法进行重写)
public static void test04(){
        TreeSet<Person> treevalueSet=new TreeSet<>(map.values());
        System.out.println(treevalueSet);
 }

因为Map中LinkedhashMap是有序的,所以将HashMap转化为LinkedHashMap可以实现排序。

  • 借助LinkedHashMap生成有序map
public static void test05(){
        TreeSet<Integer> treeSet=new TreeSet<>(map.keySet());   //借助treeset生成有序的key
        Iterator iterator=treeSet.iterator();
        HashMap<Integer,Person> helpmap=new LinkedHashMap<>();  //再利用linkedhashmap生成有序map
        while (iterator.hasNext()){
            int key=(int) iterator.next();
            Person value=map.get(key);
            helpmap.put(key,value);
        }
        System.out.println(helpmap);
    }

三、通过ArrayList

  • 仅对key进行排序
public static void test06(){
        List<Integer> mapKeys = new ArrayList<>(map.keySet());
        Collections.sort(mapKeys);
        System.out.println(mapKeys);
}
  • 仅对value进行排序
    public static void test07(){
        List<Person> mapValues = new ArrayList<>(map.values());
        Collections.sort(mapValues, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.age-o2.age;
            }
        });
        System.out.println(mapValues);
}

因为Map中LinkedhashMap是有序的,所以将HashMap转化为LinkedHashMap可以实现排序。

  • 借助LinkedHashMap生成有序map
    public static void test08(){
        List<Integer> mapKeys = new ArrayList<>(map.keySet());  //借助arraylist生成有序的key
        Collections.sort(mapKeys);   
        Iterator iterator=mapKeys.iterator();
        HashMap<Integer,Person> helpmap=new LinkedHashMap<>();  //再利用linkedhashmap生成有序map
        while (iterator.hasNext()){
            int key=(int) iterator.next();
            Person value=map.get(key);
            helpmap.put(key,value);
        }
        System.out.println(helpmap);
    }

四、通过Collections.sort()

    public static void test09(){
        List<Map.Entry<Integer,Person>> list = new ArrayList<>(map.entrySet()); //将map的entryset放入list集合
        //对list进行排序,并通过Comparator传入自定义的排序规则
        Collections.sort(list,new Comparator<Map.Entry<Integer, Person>>() {
            @Override
            public int compare(Entry<Integer, Person> o1, Entry<Integer, Person> o2) {
                if(o1.getKey()!=o2.getKey())
                    return o1.getKey()-o2.getKey();
                else return o1.getValue().age-o2.getValue().age;
            }
        });
        System.out.println(list);
    }

全部代码

import java.util.*;
import java.util.Map.Entry;

class Person{
    public int age;
    public String name;
    public Person(int age, String name) {
        this.age=age;
        this.name=name;
    }
    @Override
    public String toString() {
        return "Person{" +
                "age=" + age +
                ", name='" + name + '\'' +
                '}';
    }
}

public class Main{
    static HashMap<Integer,Person> map=new HashMap<>();
    public static void constructmap(){
        map.put(1600,new Person(16,"战肖"));
        map.put(1200,new Person(12,"张三"));
        map.put(1500,new Person(15,"李莹"));
        map.put(1300,new Person(13,"李四"));
        map.put(1400,new Person(14,"王五"));
    }

    public static void test01(){
        TreeMap<Integer,Person> treeMap=new TreeMap<>(map);
        System.out.println(treeMap);
    }

    public static void test02(){
        TreeMap<Integer,Person> treeMap=new TreeMap<>();
        treeMap.putAll(map);
        System.out.println(treeMap);
    }
    public static void test03(){
        TreeSet<Integer> treeSet=new TreeSet<>(map.keySet());
        System.out.println(treeSet);
    }
    public static void test04(){
        TreeSet<Person> treevalueSet=new TreeSet<>(map.values());
        System.out.println(treevalueSet);
    }
    public static void test05(){
        TreeSet<Integer> treeSet=new TreeSet<>(map.keySet());
        Iterator iterator=treeSet.iterator();
        HashMap<Integer,Person> helpmap=new LinkedHashMap<>();  //map中的linkedhashmap是有序的
        while (iterator.hasNext()){
            int key=(int) iterator.next();
            Person value=map.get(key);
            helpmap.put(key,value);
        }
        System.out.println(helpmap);
    }

    public static void test06(){
        List<Integer> mapKeys = new ArrayList<>(map.keySet());
        Collections.sort(mapKeys);
        System.out.println(mapKeys);
    }

    public static void test07(){
        List<Person> mapValues = new ArrayList<>(map.values());
        Collections.sort(mapValues, new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                return o1.age-o2.age;
            }
        });
        System.out.println(mapValues);
    }
    public static void test08(){
        List<Integer> mapKeys = new ArrayList<>(map.keySet());
        Collections.sort(mapKeys);
        Iterator iterator=mapKeys.iterator();
        HashMap<Integer,Person> helpmap=new LinkedHashMap<>();  //map中的linkedhashmap是有序的
        while (iterator.hasNext()){
            int key=(int) iterator.next();
            Person value=map.get(key);
            helpmap.put(key,value);
        }
        System.out.println(helpmap);
    }

    public static void test09(){
        List<Map.Entry<Integer,Person>> list = new ArrayList<>(map.entrySet()); //将map的entryset放入list集合
        //对list进行排序,并通过Comparator传入自定义的排序规则
        Collections.sort(list,new Comparator<Map.Entry<Integer, Person>>() {
            @Override
            public int compare(Entry<Integer, Person> o1, Entry<Integer, Person> o2) {
                if(o1.getKey()!=o2.getKey())
                    return o1.getKey()-o2.getKey();
                else return o1.getValue().age-o2.getValue().age;
            }
        });
        System.out.println(list);
    }



    public static void main(String[] args) {
        constructmap();
        //test01();
        //test02();
        //test03();
        //test04();
        //test05();
        //test06();
        //test07();
        //test08();
        test09();
    }
}
}

你可能感兴趣的:(Java,java,hash)