HashMap按值(时间)排序

public class Test3 {

	public static void main(String[] args) {
		Map map = new HashMap<>();
		
		Info info1 = new Info("xxxx", "hhhh", "1980-11-01");
		Info info2 = new Info("xxxx", "hhhh", "1983-11-22");
		Info info3 = new Info("xxxx", "hhhh", "1925-11-01");
		
		map.put("小杰杰", info1);
		map.put("你好", info2);
		map.put("你好1", info3);
		
		List> list = new ArrayList>(map.entrySet());
		
		Collections.sort(list, new Comparator>() {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			public int compare(Entry o1, Entry o2) {
				try {
                    Date dt1 = format.parse(o1.getValue().getTime());
                    Date dt2 = format.parse(o2.getValue().getTime());
                    if (dt1.getTime() > dt2.getTime()) {
                        return 1;
                    } else if (dt1.getTime() < dt2.getTime()) {
                        return -1;
                    } else {
                        return 0;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
				return 0;
			};
		});
		
		for(Map.Entry mapping : list){ 
            System.out.println(mapping.getKey()+" : "+mapping.getValue()); 
		} 
	}

 

你可能感兴趣的:(三年级下)