I just read Is there a no-duplicate List implementation out there? answer about a List
, NOT Set
, implementation that doesn't allow duplicates. The accepted answer recommended the Collections15 SetUniqueList
. Is there some equivalent -- in Guava perhaps? (I searched the docs and couldn't find) -- in other libs or is there some other currently popular solution?

- 1
- 1

- 33,401
- 34
- 117
- 188
-
1Is it such a complex thing so as not to spend half hour and just implement it? Really, take a `List
` implementation of your choosing and bolt a `Set – 9000 Nov 01 '11 at 00:56` to it. Consult the set on `add()` and `addAll()`; anything else can't create duplicates (unless you store mutating objects). -
Thanks @9000! No problems rolling my own, just wanted to get the lay of the land and see if there was something all the cool kids were using that I wasn't aware of. – LuxuryMode Nov 01 '11 at 01:07
-
@JarrodRoberson Yeah, but in Java-land a List must maintain element order while a Set can give you an Iterator that'll happily randomize the whole thing. – G_H Nov 01 '11 at 01:16
-
1@JarrodRoberson, nope. The superinterface of `SortedSet` is `Set` not `List` – LuxuryMode Nov 01 '11 at 16:19
-
I said it is technically, not practically. In Java ideally anywhere you technically need a `Set` (no dupes) you should use a `Set` to imply this contract pre-condition, if you need the added behavior of a `List` ( ordered iteration, etc ) then a `SortedSet` is what you should use. – Nov 01 '11 at 16:22
-
1@JarrodRoberson, it is not TECHNICALLY a List. It is PRACTICALLY a list if anything, so you're confusing your terms. – LuxuryMode Nov 01 '11 at 16:46
-
1@JarrodRoberson There's little in common between lists and sets or sortedSets that goes beyond being collections. Lists are sorted according insertion order, sets are not sorted at all and sortedSets are sorted according to the natural order of the object you store or by specifying a Comparator – monzonj Nov 21 '12 at 12:36
2 Answers
I like to require the correct Java Semantic when I have a Collection that should not have any duplicates, and that is to use the Set
interface. That said, in many cases you want to preserve the insertion order like a List
would for convenience, maintaining two parallel data structures seems wasteful and complicated to keep in sync. That is why I use something like this. InsertionOrderSet.java This is a special implementation of SortedSet
that uses a wrapper object to maintain and index
that can be sorted on by a comparator, but hides that implementation detail from the external consumers so it just looks like a regular old type safe SortedSet
.
The stuff in the answer you linked seems quite adequate. I'd go with one of those solutions. Alternatively, you could simply extend class ArrayList
and override some of the methods that do a check against a HashSet
prior to calling super.method
for that same method. Simply add the Set as an instance field, use it for checking dupes and add/remove whatever items get added to/removed from the list.

- 11,739
- 3
- 38
- 82