-1

I am using HashSet<String> to store values but some of String has the same hash code. How HashSet handles collision.

List<ClassA> getValuesFromA(){

}

List<ClassA> getValuesFromB(){

}

Set <String> a = new HashSet<String>(getValuesFromA()); // data overwritten due to hash code collision

Set <String> b = new HashSet<String>(getValuesFromB()); // data overwritten due to hash code collision

a.removeAll(b);
a.stream().forEach(t -> t.setSomeValue(X));
b.addAll(a);

I am using HashSet to find a minus b in O(1) for each element and then b + (a - b). But while storing data in HashSet some of the data are overwritten. Has anyone any idea to perform this operation without changing the hash method or data structure?

  • 3
    "But while storing data in HashSet I lost some of the data." Please be much clearer. Note that hash code collisions *don't* lose data - they just make it slightly less efficient to find values. Please provide a [mcve] as your question is unclear at the moment. – Jon Skeet May 29 '17 at 09:33
  • 3
    (You probably just want to use `retainAll`, btw.) – Jon Skeet May 29 '17 at 09:34
  • check this out: https://stackoverflow.com/questions/2851938/efficiently-finding-the-intersection-of-a-variable-number-of-sets-of-strings – Sarthak Mittal May 29 '17 at 09:43

1 Answers1

0

But while storing data in HashSet I lost some of the data. How to handle this?

Collision in hashCode() result will not loose or overwrite an object with the same hashcode in the HashSet.
Two objects with the same hashcode can be stored in a same HashSet.

In your sample code, you do that :

Set <String> a = new HashSet<String>(getValuesFromA());

Set <String> b = new HashSet<String>(getValuesFromB());

a.removeAll(b);

From a you remove all values contained in b.
So a will contains a minus b values.
You do an exclusion.

Then you do :

b.addAll(a);

It is not an intersection as you add to b only a values that are not contained in b.

You should rather use the retainAll() method as suggested by Jon Skeet and that seems to suit to your requirement :

Retains only the elements in this set that are contained in the specified collection

Set<String> intersectionSet = new HashSet<>(getValuesFromA());    

intersectionSet.retainAll(getValuesFromB());
davidxxx
  • 125,838
  • 23
  • 214
  • 215