开发者

Efficient EnumSet + List

开发者 https://www.devze.com 2022-12-30 19:09 出处:网络
Someone knows a nice so开发者_如何转开发lution for EnumSet + List I mean I need to store enum values and I also need to preserve the order , and to be able to access its index of the enum value in th

Someone knows a nice so开发者_如何转开发lution for EnumSet + List

I mean I need to store enum values and I also need to preserve the order , and to be able to access its index of the enum value in the collection in O(1) time.


The closest thing I can come to think of, present in the API is the LinkedHashSet:

From http://java.sun.com/j2se/1.4.2/docs/api/java/util/LinkedHashSet.html:

Hash table and linked list implementation of the Set interface, with predictable iteration order.


I doubt it's possible to do what you want. Basically, you want to look up indexes in constant time, even after modifying the order of the list. Unless you allow remove / reorder operations to take O(n) time, I believe you can't get away with lower than O(log n) (which can be achieved by a heap structure).


The only way I can see to satisfy ordering and O(1) access is to duplicate the data in a List and an array of indexes (wrapped in a nice little OrderedEnumSet, of course).

0

精彩评论

暂无评论...
验证码 换一张
取 消