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?
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?
Ever considered using a java.util.List instead?
Otherwise I would recommend a Multiset from e.g Google collections
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.
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.
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.
NO chance.... you can not have duplicate values in SET interface... If you want duplicates then you can try Array-List
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.
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
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.
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.