List of unique elements?

A

alejandrina

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
 
O

Oliver Wong

alejandrina said:
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?

Does LinkedHashSet solve your problem?

- Oliver
 
P

Patricia Shanahan

alejandrina said:
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

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
 
T

Thomas Schodt

alejandrina said:
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?
 
S

Steve W. Jackson

"alejandrina said:
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 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 =
 
A

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!
 
T

Thomas Schodt

alejandrina said:
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?
 
P

Patricia Shanahan

alejandrina said:
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
 
D

Daniel Pitts

alejandrina said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,249
Members
46,848
Latest member
Graciela Mitchell

Latest Threads

Top