views:

653

answers:

10

Is it possible to allow duplicate values in the Set collection?

Is there any way to make the elements unique and have some copies of them? Is there any functions for Set collection for having duplicate values in it?

+15  A: 

Ever considered using a java.util.List instead?

Otherwise I would recommend a Multiset from e.g Google collections

Schildmeijer
Is there any way to make do something with Set and having multiple values?
Johanna
@Roger, your Multiset link points to an ancient version of the source code. Here's a link to the newest javadoc instead:http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/collect/Multiset.html
Kevin Bourrillion
it needs a user name and password!!!
Johanna
Rojer is correct. Try java.util.List (preffered) some third party library.Apart from google's collections API, you can use Apache Commons Collections without username/password. See my answer below.
Gladwin Burboz
thanks for your answer
Johanna
Huh? What needs a username and password? Also I'd definitely recommend google-collections/guava over Apache Commons if only because it uses generics.
ColinD
I have no idea what asked you for a username or password... none of the links in this answer or comments did that to me.
Kevin Bourrillion
+1  A: 

I don't believe that you can have duplicate values within a set. A set is defined as a collection of unique values. You may be better off using an ArrayList.

hoffmandirt
A: 

I don't think so. The only way would be to use a List. You can also trick with function equals(), hashcode() or compareTo() but it is going to be ankward.

Kartoch
+8  A: 

The very definition of a Set disallows duplicates. I think perhaps you want to use another data structure, like a List, which will allow dups.

Is there any way to make the elements unique and have some copies of them?

If for some reason you really do need to store duplicates in a set, you'll either need to wrap them in some kind of holder object, or else override equals() and hashCode() of your model objects so that they do not evaluate as equivalent (and even that will fail if you are trying to store references to the same physical object multiple times).

I think you need to re-evaluate what you are trying to accomplish here, or at least explain it more clearly to us.

Caffeine Coma
A: 

NO chance.... you can not have duplicate values in SET interface... If you want duplicates then you can try Array-List

giri
+2  A: 

From the javadocs:

"sets contain no pair of elements e1 and e2 such that e1.equals(e2), and at most one null element"

So if your objects were to override .equals() so that it would return different values for whatever objects you intend on storing, then you could store them separately in a Set (you should also override hashcode() as well).

However, the very definition of a Set in Java is,

"A collection that contains no duplicate elements. "

So you're really better off using a List or something else here. Perhaps a Map, if you'd like to store duplicate values based on different keys.

Cuga
+2  A: 

Sun's view on "bags" (AKA multisets):

http://java.sun.com/j2se/1.4.2/docs/guide/collections/designfaq.html#3

.

Apart from google's collections API, you can use Apache Commons Collections.

Apache Commons Collections:

http://commons.apache.org/collections/

http://commons.apache.org/collections/api-3.1/org/apache/commons/collections/Bag.html

Gladwin Burboz
Those words ("Sun's view") were written by Josh Bloch a long time ago. He since changed his tune and essentially co-designed Google Collections' Multiset.
Kevin Bourrillion
What Sun essentially mean in above statement is that it is very rare for this type of scenario to occur but if some application really need something like this then it can be implemented by custom fashion or using third party library (e.g. Apache collections or Google collections) which does it rather than cluttering core API with it. And I don't think Sun has changed this view anyhow, as "bags" (AKA multisets) are still not part of JRE as of latest Java-6.
Gladwin Burboz
A: 

As mentioned choose the right collection for the task and likely a List will be what you need. Messing with the equals(), hashcode() or compareTo() to break identity is generally a bad idea simply to wedge an instance into the wrong collection to start with. Worse yet it may break code in other areas of the application that depend on these methods producing valid comparison results and be very difficult to debug or track down such errors.

VHF
A: 

You can use java.util.SetNonUnique

Johhny
A: 

This question was asked to me also in an interview. I think the answer is, ofcourse Set will not allow duplicate elements and instead ArrayList or other collections should be used for the same, however overriding equals() for the type of the object being stored in the set will allow you to manipulate on the comparison logic. And hence you may be able to store duplicate elements in the Set. Its more of a hack, which would allow non-unique elements in the Set and ofcourse is not recommended in production level code.

IndicCrusader