What is hash code ?
Hash code is integer number that is return from hash code method of object and will be used in hashing concept for collections to identify the object buckets.
Example :
Implementing hash code:
1. Hash code method needs to overridden if equals method implemented in object and it is not vice-versa.
2. Same hash value can be returned for all objects. But it is discouraged as per concept of hashing.
3. If two objects are equal by Equals method then hash code needs to be same for those objects otherwise hashing will be failed and it is suggested that field those are used in Equals method needs to be used in hash code method implementation.
4. In Hash type collections, hash code will be used to maintain same value objects in same bucket and those will be separated and identified by equals’ method.
5. If only equals method is implemented then hash code will be different for same content objects so these object keys can’t retrieve the stored values form hash collections (HashSet, Hashtable, HashMap, etc.).
Concept of hashing:
Hashing is concept to store and retrieve the objects in hash collections.
And it uses the following methods for hashing.
1. Hash code to find the object bucket
2. Equals method to find the equivalent object.
As per this concept, different objects with same hash code will be stored in same bucket and object with same value will not found other bucket if it happens, values can’t be retrieved from hash collections using these object keys.
please see following class only with equals method.
package com.lenin.test;
Output :
Hash code is integer number that is return from hash code method of object and will be used in hashing concept for collections to identify the object buckets.
Example :
public class
HashcodeImplementation {
private String name = null;
public String
getName() {
return name;
}
public void setName(String
name) {
this.name = name;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
}
Implementing hash code:
1. Hash code method needs to overridden if equals method implemented in object and it is not vice-versa.
2. Same hash value can be returned for all objects. But it is discouraged as per concept of hashing.
3. If two objects are equal by Equals method then hash code needs to be same for those objects otherwise hashing will be failed and it is suggested that field those are used in Equals method needs to be used in hash code method implementation.
4. In Hash type collections, hash code will be used to maintain same value objects in same bucket and those will be separated and identified by equals’ method.
5. If only equals method is implemented then hash code will be different for same content objects so these object keys can’t retrieve the stored values form hash collections (HashSet, Hashtable, HashMap, etc.).
Concept of hashing:
Hashing is concept to store and retrieve the objects in hash collections.
And it uses the following methods for hashing.
1. Hash code to find the object bucket
2. Equals method to find the equivalent object.
As per this concept, different objects with same hash code will be stored in same bucket and object with same value will not found other bucket if it happens, values can’t be retrieved from hash collections using these object keys.
please see following class only with equals method.
package com.lenin.test;
import
java.util.HashMap;
import java.util.Map;
public class
HashcodeImplementation {
private String name = null;
public String
getName() {
return name;
}
public
HashcodeImplementation(String name) {
this.name = name;
}
public String
toString() {
return this.name;
}
public boolean equals(Object
obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (!(obj instanceof
HashcodeImplementation)) {
return false;
}
HashcodeImplementation other =
(HashcodeImplementation) obj;
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
return true;
}
public static void main(String[]
args) {
HashcodeImplementation t1 = new
HashcodeImplementation("Lenin");
HashcodeImplementation t2 = new
HashcodeImplementation("Lenin");
System.out.println(t1.hashCode()
== t2.hashCode());
System.out.println(t1.equals(t2));
Map map = new HashMap();
map.put(t1, t1);
map.put(t2, t2);
HashcodeImplementation t3 = new
HashcodeImplementation("Lenin");
HashcodeImplementation t4 =
map.get(t3);
if (t4 != null) {
System.out.println("No need
to implement hashcode.");
} else {
System.out.println("Hey it
is null.");
}
}
}
Output :
false
true
Hey it is null.