How to add Duplicate elements in Set?

Viewed 11541

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));  
         }
    }
4 Answers

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

From the oracle docs, A set is a collection that contains no duplicate elements. To allow for duplicate elements, you can use Bags or Multisets which are unordered collections that may contain a duplicate element.

You can also get more from this StackOverflow post reasons-for-using-a-bag-in-java

We can add duplicate elements in a Set as below.

First create a class which contains attributes that we want to put in the HashSet as below.

public class Value {

    int number;
}

After that create any number of instances from this class and assign the values you want to this attribute. Finally, add all instances to the HashSet. Look at the below sample code.

public class JavaApplication22 {

    public static void main(String[] args) {
        Set<Value> numberSet = new HashSet<>();
        Value obj1 = new Value();
        obj1.number = 1;
        Value obj2 = new Value();
        obj2.number = 2;
        Value obj3 = new Value();
        obj3.number = 1;
        Value obj4 = new Value();
        obj4.number = 2;
    
        numberSet.add(obj1);
        numberSet.add(obj2);
        numberSet.add(obj3);
        numberSet.add(obj4);
    
        Iterator<Value> itr = numberSet.iterator();
        while(itr.hasNext()){
            System.out.println(itr.next().number);
        }
    }
}

I think this will work for you.

Related