In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Today, I will talk to you about how to use the hashcode method in Java. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
First of all, to understand the role of hashCode, you must first know the collections in Java.
In general, there are two types of Collection in Java, one is List and the other is Set. Do you know the difference between them? The elements in the former set are ordered and can be repeated; in the latter, the elements are out of order, but they cannot be repeated. Then there is a more serious problem: if you want to ensure that elements do not repeat, what should be the basis for judging whether two elements repeat or not? This is the Object.equals method. However, if you check each additional element, when there are many elements, there will be a lot of element comparisons later added to the collection. That is, if there are already 1000 elements in the collection, the equals method will be called 1000 times when the 1001st element is added to the collection. This will obviously greatly reduce efficiency. Therefore, Java adopts the principle of hash table. Hash is actually a person's name, because he proposed the concept of a hash algorithm, so it was named after him. The hash algorithm, also known as the hash algorithm, assigns data directly to an address according to a specific algorithm. If we explain the hashing algorithm in detail, it will require more article space, so I won't introduce it here. Beginners can understand that the hashCode method actually returns the physical address of the object store (which may not be the case).
In this way, when the collection wants to add a new element, call the element's hashCode method first, and it can be located at the physical location where it should be placed. If there is no element in this location, it can be stored directly in this location without any comparison; if there is already an element in this location, call its equals method to compare with the new element, if it is the same, it will not be stored, and if it is different, hash other addresses. So there is a problem of conflict resolution. As a result, the actual number of calls to the equals method is greatly reduced, almost once or twice.
Therefore, the Java defines the eqauls method and the hashCode method as follows:
1. If two objects are the same, then their hashCode values must be the same
2. If the hashCode of two objects is the same, they are not necessarily the same. The same object mentioned above refers to the comparison with the eqauls method.
Of course you don't have to do as required, but you will find that the same object can appear in the Set collection. At the same time, the efficiency of adding new elements will be greatly reduced.
This is an algorithm, which is mentioned in the data structure. At an address (corresponding to a hash value, which does not specifically refer to the memory address), a linked list is stored. When put a new value, calculate the hash value according to the new value, find the corresponding position, and find that the position has been squatted, then the new value will be linked to the bottom of the old value, pointing to it (next) from the old value (or vice versa. ). Please refer to HashMap.
You can think of hashcode as a large numbered bucket. If you have a good hashing algorithm, you can ensure that each different object can be placed in a different bucket, that is, each different object has a different HashCode (the same number as above). In this way, if you are looking for an object, calculate the hashcode of the object and then directly access the object in the corresponding bucket.
But if your hashing algorithm is not good, that is, any object has the same hashCode, that is to say, now there is a bucket, and all your objects have to be put in this bucket, so that when you are looking for an object, you can quickly be sure to look for it inside, but if there are tens of millions of objects in the bucket, then you need to call equals one by one. The efficiency is too low.
Hashcode is an encoding method. In Java, each object has a hashcode,Java that can be used to identify an object through this hashcode. As for the specific coding method of hashcode, it is more complex (in fact, this code can be rewritten by programmers through inheritance and interface implementation), you can refer to the data structure book. And hashtable and other structures, it is through this hash to quickly find key objects. This is their internal connection, but you don't need to know this when programming, just know that hashtable implements an unordered arrangement of elements. .
If two objects have the same value (x.equals (y) = = true), there must be the same hash code.
Because: Hash, generally translated as "hash", but also directly transliterated as "hash", is the arbitrary length of input (also known as pre-mapping, pre-maping), through the hash algorithm, transformed into a fixed length output, the output is the hash value. This transformation is a compressed mapping, that is, the space of the hash value is usually much smaller than that of the input, and different inputs may be hashed into the same output, and it is not possible to uniquely determine the input value from the hash value.
Here is the definition of the java language:
1) if the object is equal, the hashCode must be equal
2) hashCode equal objects may not be equal.
This also involves the question of how to write a custom hashCode method: the above conditions must be met. Note that not necessarily in condition 2. For details, see java doc; Effective Java for more details.
To add a brief introduction, hash is similar to a mathematical set. Each key, k can correspond to one or more values, and objects are similar to values, so "the same object" has the same key value, that is, hashCode.
After reading the above, do you have any further understanding of how to use the hashcode method in Java? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.