yegor256 yegor256 - 1 year ago 75
Java Question

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

I'm trying to find an implementation of

at the same time in Java. I want this class to allow only unique elements (as
) and preserve their order (like
). Does it exist in JDK 6?

It's important to have
List<T>#add(int, T)
so I can insert into a specific position.

Answer Source

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

EDIT: Okay, now 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<T> and an ArrayList<T>; when adding an item, check whether or not it's in the set before adding it to the list.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download