zoukankan      html  css  js  c++  java
  • java 中对hashmap进行排序

    public class HashMapSort {
        public static void main(String[] args) {
          HashMap<Integer, Student> hashMap = new HashMap<Integer,Student>();
          hashMap.put(100, new Student("姚明",32));
          hashMap.put(2, new Student("TFboys",13));
          hashMap.put(30, new Student("刘翔",28));
          
          System.out.println("排序前:");
          System.out.println(""+hashMap.toString());
          System.out.println("排序后:");
          HashMap<Integer, Student> result  = sort(hashMap);
          System.out.println(""+ result);
        }
    
        private static HashMap<Integer, Student> sort(
                HashMap<Integer, Student> hashMap) {
            //第一步:LinkedHashMap采用的是链表结构,可以实现排序,并且是hashmap的子类
            //第二步:排序可以采用集合框架中的Collections.sort(list集合,比较器);
            //该方法可以自定义比较器
            //第三步:将参数hashmap转化成list集合,这一步是最关键的,hashmap有个成员函数enrtyset,可以将hashmap转化成set集合
            //set集合和list集合都属于collection的子类,list集合的构造函数可以是collection的子类,我们可以利用set集合构建
            //list集合达到我们将hashmap转化成list集合的目的
            //第五步:将排序之后的list集合存储在LinkedHashMap中
            
            LinkedHashMap<Integer, Student> linkedHashMap = new LinkedHashMap<Integer,Student>();
            Set<Entry<Integer, Student>> entrySet = hashMap.entrySet();
            ArrayList<Entry<Integer, Student>> arrayList = new ArrayList<>(entrySet);
            Collections.sort(arrayList,new Comparator<Entry<Integer, Student>>() {
                @Override
                public int compare(Entry<Integer, Student> obj1,
                        Entry<Integer, Student> obj2) {
                    // TODO Auto-generated method stub
                    return obj1.getValue().getAge() - obj2.getValue().getAge();
                }
            });
            System.out.println(""+arrayList.toString());
            for(int i = 0 ;i < arrayList.size(); i++){
                Entry<Integer, Student> entry = arrayList.get(i);
                linkedHashMap.put(entry.getKey(), entry.getValue());
            }
            return linkedHashMap;
        }
    }
    
    public class Student  implements Serializable{
        /**
         * 
         */
        private static final long serialVersionUID = 125252L;
        private transient String name = "姚明";
        private int age;
        
        public Student(String name, int age) {
            super();
            this.name = name;
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        @Override
        public String toString() {
            return "Student [name=" + name + ", age=" + age + "]";
        }
    }

    上面中最重要的是理解下面的几个思路:

    1、   返回值是HashMap,该hashmap具有输出排序的功能,只能想到链接结构可以实现排序,想到hashmap的子类LinkedHashMap。

    2、   第二个很关键的是要实现自定义排序,第一想到的肯定是集合框架类的Collections的sort方法,该方法可以实现自定义排序

    3、   第三个是Collections只能实现对集合框架类排序,list set属于Collection集合,map属于了另外一个框架

    4、   那些要将hashmap转化成list集合才能进行排序,hashmap的成员函数entrySet可以将hashmap转化成set集合,利用set集合可以构造list集合

    5、   对list集合进行排序

    6、   将排序后的list集合存储在LinkedhashMap中,返回。

    上面这个面试题对整个集合框架类启到了一个很好的复习的作用

  • 相关阅读:
    BZOJ 2594: [Wc2006]水管局长数据加强版
    BZOJ 2049: [Sdoi2008]Cave 洞穴勘测
    html5 canvas ( 贝塞尔曲线, 一片星空加绿地 ) quadraticCurveTo, bezierCurveTo
    c#.net 接收 base64 格式的数据并解析为图片
    html5 canvas ( 绘制一轮弯月, 星空中的弯月 )
    html5 canvas ( 圆和切点曲线的绘制 ) arc, arcTo
    html5 canvas ( 图片填充样式 ) fillStyle, createPattern
    html5 canvas ( 径向渐变, 升级版的星空 ) fillStyle, createRadialGradient
    html5 canvas ( 线性渐变, 升级版的星空 ) fillStyle, createLinearGradient, addColorStop
    html5 canvas ( 图形变换矩阵 ) transform, setTransform
  • 原文地址:https://www.cnblogs.com/kebibuluan/p/7209889.html
Copyright © 2011-2022 走看看