More than 3000 questions in repository. There are more than 900 unanswered questions. Click here and help us by providing the answer. Have a video suggestion. Click Correct / Improve and please let us know.
Ans. Yes its useless if we are not going to use its objects within Hash collection, For example - HashSet , HashMap. HashCode is used internally by these collections for Search.
Help us improve. Please let us know the company, where you were asked this question :
1.Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer,
2.If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
3.It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results.
Help us improve. Please let us know the company, where you were asked this question :
Ans. There can be two different elements with the same hashcode. When two elements have the same hashcode then Java uses the equals to further differentation. So there can be one or two objects depending on the content of the objects.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Hashcode is used for bucketing in Hash implementations like HashMap, HashTable, HashSet etc. The value received from hashcode() is used as bucket number for storing elements. This bucket number is the address of the element inside the set/map. when you do contains() then it will take the hashcode of the element, then look for the bucket where hashcode points to and if more than 1 element is found in the same bucket (multiple objects can have the same hashcode) then it uses the equals() method to evaluate if object are equal, and then decide if contain() is true or false, or decide if element could be added in the set or not.
Help us improve. Please let us know the company, where you were asked this question :
Ans. Since String is immutable, its hashcode is cached at the time of creation and it doesnt need to be calculated again. This makes it a great candidate for key in a Map and its processing is fast than other HashMap key objects. This is why String is mostly used Object as HashMap keys.
Help us improve. Please let us know the company, where you were asked this question :
Q9. public class a {
public static void main(String args[]){
final String s1=""job"";
final String s2=""seeker"";
String s3=s1.concat(s2);
String s4=""jobseeker"";
System.out.println(s3==s4); // Output 1
System.out.println(s3.hashCode()==s4.hashCode()); Output 2
}
}
Ans. S3 and S4 are pointing to different memory location and hence Output 1 will be false.
Hash code is generated to be used as hash key in some of the collections in Java and is calculated using string characters and its length. As they both are same string literals, and hence their hashcode is same.Output 2 will be true.
Help us improve. Please let us know the company, where you were asked this question :
Very Frequently asked. Favorite question in walkins and telephonic interviews. Usually among first few questions. Asked in different variants. Must know for intermediate and expert professionals.Among Top 10 frequently asked questions.
Q11. What is rule regarding overriding equals and hashCode method ?