Any implementation of Ordered Set in Java?

后端 未结 10 1949
说谎
说谎 2020-12-01 09:00

If anybody is familiar with Objective-C there is a collection called NSOrderedSet that acts as Set and its items can be accessed as an Array

10条回答
  •  爱一瞬间的悲伤
    2020-12-01 09:18

    You might also get some utility out of a Bidirectional Map like the BiMap from Google Guava

    With a BiMap, you can pretty efficiently map an Integer (for random index access) to any other object type. BiMaps are one-to-one, so any given integer has, at most, one element associated with it, and any element has one associated integer. It's cleverly underpinned by two HashTable instances, so it uses almost double the memory, but it's a lot more efficient than a custom List as far as processing because contains() (which gets called when an item is added to check if it already exists) is a constant-time and parallel-friendly operation like HashSet's, while List's implementation is a LOT slower.

提交回复
热议问题