Is there an insertion order preserving Set that also implements List?

后端 未结 7 726
抹茶落季
抹茶落季 2020-12-01 03:50

I\'m trying to find an implementation of java.util.List and java.util.Set at the same time in Java. I want this class to allow only unique elements

7条回答
  •  半阙折子戏
    2020-12-01 04:35

    TreeSet is sorted by element order; LinkedHashSet retains insertion order. Hopefully one of those is what you were after.

    You've specified that you want to be able to insert at an arbitrary location, I suspect you'll have to write your own - just create a class containing a HashSet and an ArrayList; when adding an item, check whether or not it's in the set before adding it to the list.

    Alternatively Apache's commons-collections4 offers ListOrderedSet and SetUniqueList, which behave similarly and should meet the given requirements.

提交回复
热议问题