I want to add duplicate element in set, what i do? what are the method that i have to override.
public final class AddDupliacateElementInSet
{
public static void main(String[] args)
{
Set<Integer> set =new HashSet<>();
set.add(1);
set.add(1);
set.add(3);
set.add(4);
set.add(5);
set.add(6);
set.forEach(s->System.out.println(s));
}
}
If we try to insert duplicate values in a “Set”, what will happen? Do we get any complier error? Just it doesn’t add duplicate values. Boolean add (E e) – Adds the specified element to this set if it is not already present (optional operation). As, add () method returns Boolean and on adding duplicates it will return false.
You can't add duplicates, from java doc for Set.add () or do you mean addAll?: Adds the specified element to this set if it is not already present (optional operation). More formally, adds the specified element e to this set if the set contains no element e2 such that (e==null ? e2==null : e.equals (e2)).
To build a set that can hold duplicates (=a multiset by definition) from a set you could store elements with counters. I think this is the only "sane" meaning one could find in this question (some others are ruled out in the comments above already). It's still interesting because the tricky part is to make sure element matching still works.
You can use a list if you want duplicates. Set was created in first place to have distinct elements – Chaitanya Waikar Jun 19 '19 at 5:30 I know that the set is not allowing duplicate.
As community has pointed out in the comments, a Set
is not meant to store duplicate values. But for reasons like "interview question" or "library code that you can't change", you can force it to store duplicates by overriding equals
to always return false
.
For primitives or other objects not under your control, you can create a wrapper class around your value object and make its equals()
always return false
:
public class UnequalWrapper {
private int value;
//...constructor, getter, setter
@Override
public boolean equals(Object o) {
return false;
}
@Override
public int hashCode() {
return Objects.hashCode(value);
}
}
and then use it:
public static void main(String[] args) {
UnequalWrapper a = new UnequalWrapper(1);
UnequalWrapper b = new UnequalWrapper(2);
UnequalWrapper c = new UnequalWrapper(1);
Set<UnequalWrapper> set = Set.of(a, b, c);
set.forEach(wrapper -> System.out.println(wrapper.getValue()));
}
Output:
1
2
1
But again, this is not recommended, do not try this at home!
Edit:
If you are using a wrapper, you can omit equals()
and hashcode()
. This is because the Object
class' methods will check for object reference, which will be different if you are creating a new wrapper instance each time.
Any Set
implementations in Java Collections don't allow to do that.
Consider using MultiSet
from Apache Commons Collections.
import org.apache.commons.collections4.MultiSet;
import org.apache.commons.collections4.multiset.HashMultiSet;
public class Test {
public static void main(String[] args) {
MultiSet<Integer> set = new HashMultiSet<>();
set.add(1);
set.add(1);
set.add(3);
set.add(4);
set.add(5);
set.add(6);
set.forEach(s -> System.out.println(s));
}
}
Result:
1
1
3
4
5
6
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With