Sorting a map by date key in Java

后端 未结 2 468
夕颜
夕颜 2021-01-27 07:11

I\'m trying to sort a map in java by date key using TreeMap. Here\'s my code

public static void sort() {

    BufferedReader br;
    String line;
    String[] da         


        
2条回答
  •  难免孤独
    2021-01-27 07:52

    It looks like the lines

    data = line.split(":"); map.put(df.parse(data[1]), line);

    Are effectively parsing out only the month. Line.split(":") is going to produce an array split by : . If you have dates formatted in your data file dd:mm:yyyy then the resulting array "data" should be {[dd], [mm], [yyyy]}. So data[1] is going to simply be the month.

    I could be wrong, but I suspect this is why you are only getting the 12 key-value pairs; you are parsing only for the month, and any time you get a new month key, you're overwriting the old key.

提交回复
热议问题