answersLogoWhite

0

What is hashcode in java?

Updated: 8/10/2023
User Avatar

Wiki User

14y ago

Best Answer

hashcode is an integer number which is provide to each object by jvm note that this is not address of object but for convencing internally they use

Java uses the hash function given below:- s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] where s[i] is the ith character of the string, n is the length of the string, and ^ indicates exponentiation.

User Avatar

Wiki User

14y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

HashMap

The HashMap gives you an unsorted, unordered Map. When you need a Map and you don't care about the order (when you iterate through it), then HashMap is the way to go; the other maps add a little more overhead. Where the keys land in the Map is based on the key's hashcode, so, like HashSet, the more efficient your hashCode() implementation, the better access performance you'll get. HashMap allows one null key and multiple null values in a collection.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

Hashcode indentifies the location of a object in a hashtable, equals is a method that allows to compare two objects instead of comparing just the adress at memory, with the == operator. You also can customize both.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Hashcodes are typically used to increase the performance of large collections of data. The hashCode value of an object is used by some collection classes (Don't worry about Collections, its gonna be the very next chapter). Although you can think of it as kind of an object ID number, it isn't necessarily unique. Collections such as HashMap and HashSet use the hashCode value of an object to determine how the object should be stored in the collection, and the hashCode is used again to help locate the object in the collection. For the exam you do not need to understand the deep details of how the collection classes that use hashing are implemented, but you do need to know which collections use them. You must also be able to recognize an appropriate or correct implementation of hashCode(). This does not mean legal and does not even mean efficient. It's perfectly legal to have a terribly inefficient hashCode method in your class, as long as it doesn't violate the contract specified in the Object class documentation. So for the exam, if you're asked to pick out an appropriate or correct use of hashCode, don't mistake appropriate for legal or efficient.

Understanding Hashcodes

In order to understand what's appropriate and correct, we have to look at how some of the collections use hashcodes.

Imagine a set of buckets lined up on the floor. Someone hands you a piece of paper with a name on it. You take the name and calculate an integer code from it by using A is 1, B is 2, and so on, and adding the numeric values of all the letters in the name together. A given name will always result in the same code.

Let's look at an example:

KeyHash Code AlgorithmHash CodeRockyR(18) + o(15) + c(3) + k(11) + y(25)72AnandA(1) + n(14) + a(1) + n(14) + d(4)34

The above is a simple algorithm that just sums up the numeric position of the alphabets in the name and arrives at a number that being 72 for Rocky and 34 for Anand. So in the hashCode bucket, the value 34 will be saved as the key for Anand and similarly 72 will be the value for Rocky.

Now imagine that someone comes up and shows you a name and says, "Please retrieve the piece of paper that matches this name." So you look at the name they show you, and run the same hashCode-generating algorithm. The hashCode tells you in which bucket you should look to find the name.

You might have noticed a little flaw in our system, though. Two different names might result in the same value. For example, the names Amy and May have the same letters, so the hashCode will be identical for both names. That's acceptable, but it does mean that when someone asks you (the bucket-clerk) for the Amy piece of paper, you'll still have to search through the target bucket reading each name until we find Amy rather than May. The hashCode tells you only which bucket to go into, but not how to locate the name once we're in that bucket.

So for efficiency, your goal is to have the papers distributed as evenly as possible across all buckets. Ideally, you might have just one name per bucket so that when someone asked for a paper you could simply calculate the hashCode and just grab the one paper from the correct bucket (without having to go flipping through different papers in that bucket until you locate the exact one you're looking for). The least efficient (but still functional) hashCode generator would return the same hashCode (say, 42) regardless of the name, so that all the papers landed in the same bucket while the others stood empty. The bucket-clerk would have to keep going to that one bucket and flipping painfully through each one of the names in the bucket until the right one was found. And if that's how it works, they might as well not use the hashcodes at all but just go to the one big bucket and start from one end and look through each paper until they find the one they want.

This distributed-across-the-buckets example is similar to the way hashcodes are used in collections. When you put an object in a collection that uses hashcodes, the collection uses the hashCode of the object to decide in which bucket the object should land. Then when you want to fetch that object, you have to give the collection a reference to an object that the collection compares to the objects it holds in the collection. As long as the object you're trying to search for has the same hashCode as the object you're using for the search (the name you show to the person working the buckets), then the object will be found. But...and this is a Big But, imagine what would happen if, going back to our name example, you showed the bucket-worker a name and they calculated the code based on only half the letters in the name instead of all of them. They'd never find the name in the bucket because they wouldn't be looking in the correct bucket!

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

The hashCode() and equals() are Java methods found in Object so all objects inherit these methods. A rule of thumb states that when one of these 2 methods is overridden the other should also be overridden. The default hashCode() method returns an int representation of the object's location in memory. The default equals() method compares memory locations (this == obj).

Both methods come into play when working with hash-related containers (e.g., HashSet, HashMap). These objects use a collection of buckets into which key/value entries are stored. When storing the key/value entry the hash code of the key is used to determine into which bucket the entry will be stored. When retrieving an entry the key's hash code is used to find the correct bucket.


It is possible that hash code of different keys calculate to the same value. When this happens we call it a "collision". A bucket is a collection so more than one entry can be added to a given bucket. When you try to add a key/value entry and there is a collision the entry is appended to the bucket. When retrieving an entry, the key is used to find the bucket but then it searches all entries in the bucket by comparing the keys of each entry using equals().


Note that both the key and value are stored in the bucket as an Map.Entry object so that the keys can be compared in the event of a collision.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is hashcode in java?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the hashCode method and what is the purpose of this method in Object?

The hashCode method is used to create a unique identification number to describe the state and type of an object.The Java API description of the method is:Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.The general contract of hashCode is: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, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.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.It is not required that if two objects are unequal according to the Object.equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)


Why must you override the hashcode method while overriding the equals method?

Because, if two objects are supposed to be equal as per the equals() method, then the value returned by the hashCode() method must also be the same. This will not be the case if you override only the equals method and this can have some confusing effects when using those objects with hash related collections. So it is always a good idea to override the hashCode() method if you are providing an implementation for the equals method.


Does Visual Java plus plus and Java Builder is different from the Java language?

Yes!Visual Java plus plus and Java Builder is different from the Java language?


What are the different java technologies?

There are several types of Java technology. Some examples of Java software are Java ME, Java EE, Java SE, and Java Card. Java made the JAVA development kit for those that develop in Java. There is also Java Virtual machine and some class libraries. Java is also famous for its languages like Clojure, Beanshell, Groovy, Gosu, Rhino, Kotlin, JRuby, Scala, and Jython.


Small Java-based programs are called?

Java applets

Related questions

What is the difference between reference and hashcode?

There is no relation between reference and hascode, Java reference is unique pointer which refers an object. so each object will have a unique reference. but 2 diff object can have same hashcode.


What is the hashCode method and what is the purpose of this method in Object?

The hashCode method is used to create a unique identification number to describe the state and type of an object.The Java API description of the method is:Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by java.util.Hashtable.The general contract of hashCode is: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, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.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.It is not required that if two objects are unequal according to the Object.equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hashtables.As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)


What is the difference between reference and hash code in java?

Cat oldCat = new Cat(); Cat newCat = new Cat(); Cat oldCatRef = oldCat; In the above example, oldCat and oldCatRef are references to the same object. Since they refer to the same object, their hashcodes will be equal. But oldCat and newCat do not refer to the same object. They are references to two different objects. But they might have the same hashCode based on their implementation. hashCode is simply a method in Object class which you can override.


Why must you override the hashcode method while overriding the equals method?

Because, if two objects are supposed to be equal as per the equals() method, then the value returned by the hashCode() method must also be the same. This will not be the case if you override only the equals method and this can have some confusing effects when using those objects with hash related collections. So it is always a good idea to override the hashCode() method if you are providing an implementation for the equals method.


What Indonesian island is Jakarta on?

java


Does Visual Java plus plus and Java Builder is different from the Java language?

Yes!Visual Java plus plus and Java Builder is different from the Java language?


What are the different java technologies?

There are several types of Java technology. Some examples of Java software are Java ME, Java EE, Java SE, and Java Card. Java made the JAVA development kit for those that develop in Java. There is also Java Virtual machine and some class libraries. Java is also famous for its languages like Clojure, Beanshell, Groovy, Gosu, Rhino, Kotlin, JRuby, Scala, and Jython.


Small Java-based programs are called?

Java applets


Differences between Java Applet and Java Beans?

Java applet is a program used to run java applications while beans is a compiler used to design java programs (IDE, GUI) :-) GilbertC


Who create Java and when?

Who create Java & when? Why he create java ? What are mane functions of it?


Supermost package of java?

The supermost package of Java is the "java" package.


What do you call java and javascript?

Well you get java as java and javascript as iava.