Hi all,
Unless I'm mistaken, there is no SDK implementation for a List of
unique elements. All List implementations accept duplicates; Set
implementations do not. However, having a List of unique elements is a
useful object.
Any insights? Does what I want exist anyhwere?
Thanks!
Alejandrina
Oliver Wong - 04 Jan 2007 22:07 GMT
> Hi all,
>
[quoted text clipped - 4 lines]
>
> Any insights? Does what I want exist anyhwere?
Does LinkedHashSet solve your problem?
- Oliver
alejandrina - 04 Jan 2007 22:38 GMT
Yes, I was trying to avoid the link overhead, though.
> > Hi all,
> >
[quoted text clipped - 8 lines]
>
> - Oliver
Patricia Shanahan - 04 Jan 2007 22:10 GMT
> Hi all,
>
[quoted text clipped - 8 lines]
>
> Alejandrina
As far as I know, there are no List implementations with unique
elements. However, there are Set implementations with ordering, so you
can combine order and uniqueness.
LinkedHashSet provides order-of-appearance.
TreeSet provides order by either natural order of Comparable data, or by
Comparator. Note that the order must be consistent with equals or it may
not preserve uniqueness.
Patricia
Thomas Schodt - 04 Jan 2007 22:19 GMT
> Unless I'm mistaken, there is no SDK implementation for a List of
> unique elements. All List implementations accept duplicates; Set
> implementations do not. However, having a List of unique elements is a
> useful object.
>
> Any insights? Does what I want exist anywhere?
Both List and Set are Collections.
List getSetAsList(Set values) {
return Collections.unModifiableList(new ArrayList(values));
}
But why do you want a List rather than a Set?
Steve W. Jackson - 04 Jan 2007 22:29 GMT
> Hi all,
>
[quoted text clipped - 8 lines]
>
> Alejandrina
I also don't think there is a built-in List implementation with this
restriction, but it would be dirt simple to build a class of your own
that either extends an existing List implementation or carries its own
List, and then responds to attempts to put items in by first checking
whether the new element already appears in the List.
= Steve =

Signature
Steve W. Jackson
Montgomery, Alabama
alejandrina - 04 Jan 2007 22:42 GMT
I guess I should have said that I wanted a List implementation to avoid
the overheads of linked sets and hash sets when I did not need them, as
all we want is the get(index) method.
We can certainly write an enhanced ArrayList to do what we want, but
does it exist already in a 3rd party library?
Thanks all for the quick answers!
Alejandrina
> Hi all,
>
[quoted text clipped - 8 lines]
>
> Alejandrina
Thomas Schodt - 04 Jan 2007 23:07 GMT
> I guess I should have said that I wanted a List implementation to avoid
> the overheads of linked sets and hash sets when I did not need them, as
> all we want is the get(index) method.
>
> We can certainly write an enhanced ArrayList to do what we want, but
> does it exist already in a 3rd party library?
If you need to be able to insert values into your Collection,
how are you going to enforce your constraints in an enhanced ArrayList
implementation? I would think your implementation would incur
unreasonable overhead on insertion unless you use a HashSet?
Patricia Shanahan - 04 Jan 2007 23:16 GMT
> I guess I should have said that I wanted a List implementation to avoid
> the overheads of linked sets and hash sets when I did not need them, as
> all we want is the get(index) method.
I don't understand that comment in the context of requiring enforced
uniqueness. A collection with enforced uniqueness must do a search by
value for every insert operation, to find out if there is already an
equal item. If you don't have structure such as a tree or a hash map,
that search becomes extremely expensive.
If you can ensure uniqueness, then any List is fine.
Patricia
Daniel Pitts - 05 Jan 2007 00:32 GMT
> Alejandrina
> > Hi all,
[quoted text clipped - 17 lines]
>
> Thanks all for the quick answers!
Hmm, Do you need to get the index? Or do you want to go through all of
them in order?
If you only want to get them in order, then using LinkedHashSet and
iteration is the way to go.
If you REALLY need no duplicates, AND really need to get by index, you
could do it this way:
Set<T> set;
List<T> list;
...
if (set.add(obj)) {
list.add(obj)
};
Or, if you build the set once, then use a LinkedHashSet, and then call
toArray(), or new ArrayList(set))
Hope these suggestions help.
- Daniel