Java Class that implements Map and keeps insertion order?

后端 未结 9 2289
一向
一向 2020-11-22 11:28

I\'m looking for a class in java that has key-value association, but without using hashes. Here is what I\'m currently doing:

  1. Add values to a Hashtable<
9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 11:53

    Whenever i need to maintain the natural order of things that are known ahead of time, i use a EnumMap

    the keys will be enums and you can insert in any order you want but when you iterate it will iterate in the enum order (the natural order).

    Also when using EnumMap there should be no collisions which can be more efficient.

    I really find that using enumMap makes for clean readable code. Here is an example

提交回复
热议问题