Sorting a HashMap by date

后端 未结 6 1996
隐瞒了意图╮
隐瞒了意图╮ 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:21

    1. Get all Entries by calling entrySet() method of Map

    2. Create a custom Comparator to sort entries based upon values

    3. Convert Entry set to List

    4. Sort Entry list by using Collections.sort() method by passing your value comparator

    5. Create a LinkedHashMap by adding entries in sorted order.

    Look at example code @ Sort HasMap by value

提交回复
热议问题