MAP中的比较器与日期的排序

package cn.Map.Test;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import cn.Demo.Pojo.Student;
public class MapTest {
    public static void main(String[] args) {
        Demo1();
        Demo2();    
    }

    private static void Demo2() {
        Map<String,String> map=new HashMap<>();
        map.put("i", "a");
        map.put("ii", "b");
        map.put("iii", "c");
        map.put("IV", "d");
        map.put("V", "e");

        ArrayList<Map.Entry<String, String>> alist = new ArrayList<>(map.entrySet());
        //方法一
        alist.sort(new Comparator<Map.Entry<String, String>>(){

            @Override
            public int compare(Entry<String, String> o1, Entry<String, String> o2) {

                return o1.getValue().compareTo(o2.getValue());
            }

        });
        //方法二
        /*Collections.sort(alist, new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {

                return o1.compareTo(o2);
            }
        });*/


            System.out.println(alist);
    }

    private static void Demo1() {
        Map<String,Student> stmap=new HashMap<>();
        stmap.put("二",new Student(2,"李晓东","男","辽宁沈阳",Date.valueOf("2017-01-01"),200L));
        stmap.put("一",new Student(1,"杨小华","男","辽宁大连",Date.valueOf("2016-01-02"),100L));
        stmap.put("三",new Student(3,"纪丽君","女","辽宁阜新",Date.valueOf("2015-01-04"),100L));
        stmap.put("四",new Student(4,"冯知财","男","辽宁营口",Date.valueOf("2014-01-03"),400L));
        stmap.put("五",new Student(5,"刘丽枫","女","辽宁锦州",Date.valueOf("2013-01-06"),300L));
        stmap.put("零",new Student(6,"江鲁花","女","辽宁丹东",Date.valueOf("2012-01-05"),100L));


        List<Entry<String, Student>> list=new ArrayList<>(stmap.entrySet());

    /*  list.sort(new Comparator<Entry<String, Student>>(){

            @Override
            public int compare(Entry<String, Student> o1, Entry<String, Student> o2) {

                return (int) (o1.getValue().getId()-o2.getValue().getId());
            }});*/

        Collections.sort(list,new Comparator<Entry<String, Student>>(){

            @Override
            public int compare(Entry<String, Student> o1, Entry<String, Student> o2) {

                //return (int) (o1.getValue().getId()-o2.getValue().getId());
                    //*对比两个字符串
                //return o1.getValue().getName().compareTo(o2.getValue().getName());
                    //*将时间转化成毫秒并比较
                //return String.valueOf(o1.getValue().getBirthday().getTime()).compareTo(String.valueOf(o2.getValue().getBirthday().getTime()));
                    //*比较时间中的“日”    
                //return o1.getValue().getBirthday().getDate()-o2.getValue().getBirthday().getDate();
                //*比较时间 
                return o1.getValue().getBirthday().compareTo(o2.getValue().getBirthday());  

            }

        });

        for (Entry<String, Student> e : list) {
            System.out.println(e);

        }
    }

}

猜你喜欢

转载自blog.csdn.net/konghaifengzhu/article/details/81201601