Sort date strings in map [duplicate]

醉酒当歌 提交于 2019-12-13 00:55:27

问题


Hi I want to sort map objects based on dates below is the map

    testMap.put("06/15/2015", 1);
    testMap.put("05/15/2015", 2);
    testMap.put("01/15/2016", 4);
    testMap.put("07/15/2015", 3);
    testMap.put("02/15/2016", 5);

I need output like below sequence:

05/15/2015 06/15/2015 07/15/2015 01/15/2016 02/15/2016

I need ouput like sorted months in 2015 and then starting 2016 months in sorted order

I have tried using treemap but it used to work if dates strings are in Date format, Can any one help in sorting as above sequence


回答1:


Create a custom comparator for the TreeMap:

class DateComparator implements Comparator<String>, Serializable {

    public int compare(String date1, String date2) {
        int date1Int = convertDateToInteger(date1);
        int date2Int = convertDateToInteger(date2);

        return date1Int - date2Int;
    }

    // converts date string with format MM/DD/YYYY to integer of value YYYYMMDD
    private int convertDateToInteger(String date) {
        String[] tokens = date.split("/");

        return Integer.parseInt(tokens[2] + tokens[0] + tokens[1]);
    }
}

TreeMap<String, Integer> treeMap = new TreeMap<>(new DateComparator());

This has the benefit of avoiding Date objects, which may slow put operations down.

Date strings like 06/15/2015 or 05/15/2015 are converted to comparable integer equivalents (20150615 and 20150515 respectively) in keeping with their chronological order.

You can then add key/value pairs as usual, the keys will be ordered as per the comparator:

treeMap.put("06/15/2015", 1);
treeMap.put("05/15/2015", 2);
treeMap.put("01/15/2016", 4);
treeMap.put("07/15/2015", 3);
treeMap.put("02/15/2016", 5);



回答2:


You can do this by implementing a Comparator<String> and telling TreeMap about it when you create a new instance...

public class DateAsStringComparator implements Comparator<String>, Serializable {
   public int compareTo(String o1, String o2) {
     // Convert to Date and compare here.
   }      
}

And then...

final TreeMap<String, Integer> testMap = new TreeMap<>(new DateAsStringComparator());

(Code is untested but should give you an outline). Be sure to have your Comparator implement Serializable if you plan on serializing the TreeMap!




回答3:


Actually Todd's answer is right to use TreeMap, I just added the implementation of the comparator. Better to delegate date comparison to "compareTo" methods built in java.util.Date

public class MyDateComparator implements Comparable<String>
{

    public int compareTo( String d1, String d2 )
    {
       if( d1.matches("\\d{2}\\/\\d{2}\\/\\d{4}") && d2.matches("\\d{2}\\/\\d{2}\\/\\d{4}") )
       { 
         //Valid date format
         String[] date_contents =  d1.split("/");
         java.util.Date date1 = new java.util.Date(date_contents[2], date_contents[0], date_contents[1] );
         date_contents =  d2.split("/");
         java.util.Date date2 = new java.util.Date(date_contents[2], date_contents[0], date_contents[1] );
         return date1.compareTo(date2);
       }

       return d1.compareTo(d2);
   }



});

Map<String, String> treeMap = new TreeMap<String, String>(new MyDateComparator());


来源:https://stackoverflow.com/questions/35416679/sort-date-strings-in-map

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!