Sorting a HashMap by date

后端 未结 6 1989
隐瞒了意图╮
隐瞒了意图╮ 2021-02-15 16:00

In a Java class I have a method to reOrder an existing HashMap by date. The HashMap is of a type where the Object contains a field called exp

6条回答
  •  囚心锁ツ
    2021-02-15 16:16

    Your best bet will be to use a SortedMap with the Comparator interface.

    Here is an example:

    public SortedMap getSortedMap(Map originalMap) {
        SortedMap tmpMap = new TreeMap(new Comparator(){
            @Override
            public int compare(String key1, String key2) {
                //logic for comparing dates
            }           
        });
        tmpMap.putAll(originalMap);
        return tmpMap;
    }
    

提交回复
热议问题