Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HashSet Collisions in Java

I have a program for my Java class where I want to use hashSets to compare a directory of text documents. Essentially, my plan is to create a hashSet of strings for each paper, and then add two of the papers hashSets together into one hashSet and find the number of same 6-word sequences.

My question is, do I have to manually check for, and handle, collisions, or does Java do that for me?

like image 862
marcinx27 Avatar asked Oct 16 '12 07:10

marcinx27


People also ask

Can HashSet have collisions?

Java Hash Maps/Sets Automatically handle Hash collisions, this is why it is important to override both the equals and the hashCode methods. As both of them are utilised by Sets to differentiate duplicate or unique entries.

How are collisions handled with a HashMap in Java?

1) HashMap handles collision by using a linked list to store map entries ended up in same array location or bucket location. 2) From Java 8 onwards, HashMap, ConcurrentHashMap, and LinkedHashMap will use the balanced tree in place of linked list to handle frequently hash collisions.

Is HashSet faster than HashMap?

HashMap is faster/ than HashSet because values are associated with a unique key. HashSet is slower than HashMap because the member object is used for calculating hashcode value, which can be same for two objects. Only one object is created during the add operation.

What is faster than HashSet in Java?

HashMap is faster than HashSet because the values are associated to a unique key. In HashSet , member object is used for calculating hashcode value which can be same for two objects so equals() method is used to check for equality.


1 Answers

Java Hash Maps/Sets Automatically handle Hash collisions, this is why it is important to override both the equals and the hashCode methods. As both of them are utilised by Sets to differentiate duplicate or unique entries.

It is also important to note that these hash collisions hava a performance impace since multiple objects are referenced by the same Hash.

public class MyObject {
private String name;

//getter and setters


public int hashCode() {
   int hashCode = //Do some object specifc stuff to gen hashCode
   return int;
}

public boolean equals(Object obj) {
   if(this==obj) return true;
   if(obj instanceOf MyObject) {
       if(this.name.equals((MyObject)obj.getName())) {
           return true;
       }
   return false;
}
}
}

Note: Standard Java Objects such as String have already implemented hashCode and equals so you only have to do that for your own kind of Data Objects.

like image 155
dngfng Avatar answered Oct 04 '22 17:10

dngfng