java中map如何实现key排序(如何利用java中的list和map对时间进行排序)
java中map如何实现key排序(如何利用java中的list和map对时间进行排序)private void testSort(){ List<Calendar> cals=new ArrayList<Calendar>(); Calendar cal3=Calendar.getInstance(); cal3.add(Calendar.DATE 2); cal3.set(cal3.get(Calendar.YEAR) cal3.get(Calendar.MONTH) cal3.get(Calendar.DAY_OF_MONTH) 0 0 0); Calendar cal4=Calendar.getInstance(); cal4.add(Calendar.DATE 2); cal4.set(cal4.get(Calendar.YEAR) cal4.get(Calendar.MONTH) cal4.get(C
项目需要,放到list的数据,想让按照时间排序,该怎么处理呢?
最初设想使用:时间long型
private void testTimes() throws InterruptedException{ Calendar cal=Calendar.getInstance(); cal.add(Calendar.DATE 1); cal.set(cal.get(Calendar.YEAR) cal.get(Calendar.MONTH) cal.get(Calendar.DAY_OF_MONTH) 0 0 0); Long time1=cal.getTimeInMillis(); Calendar ca2=Calendar.getInstance(); ca2.add(Calendar.DATE 1); ca2.set(cal.get(Calendar.YEAR) cal.get(Calendar.MONTH) cal.get(Calendar.DAY_OF_MONTH) 0 0 0); Long time2=ca2.getTimeInMillis(); System.out.println(time1 "--" time2); }
发现整型可能不一致,即有时候一致,有时候不一致。
经测试发现,Collections提供了排序功能:
private void testSort(){ List<Calendar> cals=new ArrayList<Calendar>(); Calendar cal3=Calendar.getInstance(); cal3.add(Calendar.DATE 2); cal3.set(cal3.get(Calendar.YEAR) cal3.get(Calendar.MONTH) cal3.get(Calendar.DAY_OF_MONTH) 0 0 0); Calendar cal4=Calendar.getInstance(); cal4.add(Calendar.DATE 2); cal4.set(cal4.get(Calendar.YEAR) cal4.get(Calendar.MONTH) cal4.get(Calendar.DAY_OF_MONTH) 0 0 0); Calendar cal1=Calendar.getInstance(); cal1.add(Calendar.DATE 2); cal1.set(cal1.get(Calendar.YEAR) cal1.get(Calendar.MONTH) cal1.get(Calendar.DAY_OF_MONTH) 0 0 2); Calendar cal2=Calendar.getInstance(); cal2.add(Calendar.DATE 2); cal2.set(cal2.get(Calendar.YEAR) cal2.get(Calendar.MONTH) cal2.get(Calendar.DAY_OF_MONTH) 0 0 0); cals.add(cal3); cals.add(cal4); cals.add(cal1); cals.add(cal2); Calendar minTime=Collections.min(cals); System.out.println(DateUtils.formatDate(Collections.min(cals).getTime() "yyyy-MM-dd HH:mm:ss")); for(int i=0;i<cals.size();i ){ if(DateUtils.defaultFormatDate(minTime.getTime()).equals(DateUtils.defaultFormatDate(cals.get(i).getTime()))){ System.out.println(DateUtils.formatDate(cals.get(i).getTime() "yyyy-MM-dd HH:mm:ss")); } } /* for(int i=0;i<cals.size();i ){ System.out.println(DateUtils.formatDate(cals.get(i).getTime() "yyyy-MM-dd HH:mm:ss")); } Collections.sort(cals); //排序后 for(int i=0;i<cals.size();i ){ System.out.println(DateUtils.formatDate(cals.get(i).getTime() "yyyy-MM-dd HH:mm:ss")); }*/ }
再来考虑map的时间排序如何做?
hashMap排序,示例:
private void test(){ Map<String List<String>> unSupportedDatesMap=new HashMap<String List<String>>(); unSupportedDatesMap.put("14:00-18:00" new ArrayList<String>()); unSupportedDatesMap.put("9:00-12:00" new ArrayList<String>()); unSupportedDatesMap.put("19:00-22:00" new ArrayList<String>()); List<Map.Entry<String List<String>>> intervals = new ArrayList<Map.Entry<String List<String>>>(unSupportedDatesMap.entrySet()); //排序前 System.out.println("------------------排序前--------------------------"); for (int i = 0; i < intervals.size(); i ) { String id = intervals.get(i).toString(); System.out.println(id); } //排序 Collections.sort(intervals new Comparator<Map.Entry<String List<String>>>() { public int compare(Map.Entry<String List<String>> o1 Map.Entry<String List<String>> o2) { String intervalTime1 = o1.getKey(); String fromTime1 =intervalTime1.split("-")[0]; String intervalTime2 = o2.getKey(); String fromTime2 =intervalTime2.split("-")[0]; //转换为分钟值 String hour1=fromTime1.split(":")[0].trim(); String mininute1=fromTime1.split(":")[1].trim(); int times1=60*Integer.parseInt(hour1) Integer.parseInt(mininute1); String hour2=fromTime2.split(":")[0].trim(); String mininute2=fromTime2.split(":")[1].trim(); int times2=60*Integer.parseInt(hour2) Integer.parseInt(mininute2); return (times1 - times2); } }); //排序后 System.out.println("------------------排序后--------------------------"); for (int i = 0; i < intervals.size(); i ) { String id = intervals.get(i).toString(); System.out.println(id); } }
list和map的时间排序就到这里了,原理下次来追。