Sorting a HashMap by date

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

    The correct solution depends on your performance constraints.

    If your issue is just finding the item with the newest date, then if O(n) performance is OK you can do a scan of the values() in your HashMap and find the minimum that way.

    It depends on how often you need to do this relative to other access on the data structure. It would be perfectly reasonable to use a SortedMap or use a secondary data structure such as a PriorityQueue (acting as a heap on the date), depending on your access patterns for this data structure.

提交回复
热议问题