Any implementation of Ordered Set in Java?

Uko picture Uko · Jan 3, 2012 · Viewed 114.3k times · Source

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's ones.

Is there anything like this in Java?

I've heard there is a collection called LinkedHashMap, but I haven't found anything like it for a set.

Answer

Chandra Sekhar picture Chandra Sekhar · Jan 3, 2012

Take a look at LinkedHashSet class

From Java doc:

Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set. (An element e is reinserted into a set s if s.add(e) is invoked when s.contains(e) would return true immediately prior to the invocation.).