Re: List of unique elements?
alejandrina wrote:
Alejandrina
alejandrina wrote:
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
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!
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