There are several very important methods in the java.lang.object class, and today we’ll discuss the hashCode() method.

What is a Hash

A Hash algorithm can also be called a Hash algorithm. A method that uses the Hash algorithm to generate strings or numbers can be called a Hash algorithm or Hash algorithm.

If that doesn’t make sense, consider MD5. MD5 is a typical hash algorithm, with MD5 algorithm, whether you input string, image, binary file, you can get a string.

The algorithm that gets this string is the Hash algorithm.

Why Hash

We’re not going to talk about complex Hash algorithms here or how Hash algorithms compute. Because then it might take you a long time to figure out how to do it.

The purpose of using the Hash algorithm is to disperse the extracted data summary information as much as possible and avoid duplication. At the same time, it is necessary to ensure that the Hash results of the same data are unchanged.

No matter how many times you Hash the same data, as long as the data is the same, the Hash must be the same.

Hash collision

In real life, the Hash results for different data might be the same.

Look at the following code:

        logger.debug("HashCode AaAaAa - {}", "AaAaAa".hashCode());
        logger.debug("HashCode BBAaBB - {}", "BBAaBB".hashCode());
Copy the code

The above code outputs the same result, which is called a Hash collision.

Unfortunately, this hash collision is unavoidable in reality.

Common hash algorithms

Common Hash algorithms include the following.

MD5 is no longer a secure Hash algorithm. Sha-256 is gradually recommended for cryptography and development.

algorithm

Output length (bits)

Output length (bytes)

MD5

128 bits

16 bytes

SHA-1

160 bits

20 bytes

RipeMD-160

160 bits

20 bytes

SHA-256

256 bits

32 bytes

SHA-512

512 bits

64 bytes

According to the collision probability, the longer the output length of the hash algorithm, the harder it is to generate collisions, and the safer it is.

Java hashCode ()

The Java hashCode() method returns data of type int.

The following uses the hashCode of a String as an example.

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1]
Copy the code

There will be different calculations depending on whether the input string is UTF16 or not.

IntelliJ IDEA Rewriting Hash algorithm

If you don’t want to use the Hash algorithm in Java Objects, you can override the Hash algorithm in your objects.

Enter Alt+Insert in IntelliJ IDEA, which will bring up the option for a quick generation method.

You will then be prompted with your choice of method to create the hashCode() method. You can choose to use JDK built-in methods, or you can choose to override methods using Apache Commons-lang methods. Of course, there are other ways you can write it, and the principle is the same regardless of which way you write it.

Select variables. After completing the method selection above, you will be prompted to select variables.

Select the variables that will need to be created and then next.

It also requires you to select non-empty fields, which you can choose by default or not.

Below, you can see the default hashCode method generated using the JDK.

    @Override
    public int hashCode() {
        return Objects.hash(title, topic_id, raw, category, target_recipients, archetype, created_at);
    }
Copy the code

If you use Apache Commons, the results will be different.

You can explore it yourself in the IDE.

Note that in hashCode, you might see the numbers 17,31,37.

In fact, these numbers are prime numbers, in the Java interview there may be a question to find a prime number within 100.

Because Hash algorithm can also be used in cryptography in many cases, a lot of basic research in cryptography is the study of prime numbers.

RSA algorithm widely used in network is an important application based on prime properties.

So in the hashCode method, you can see the numbers above, and this is one of the practical applications of the prime algorithm. Because there’s a lot of cryptography involved, we won’t actually expand it here.

With the above instructions, we can Hash objects or data in Java.

Hash algorithms and applications are the foundation of Java Hashmap, so hashCode methods will also exist as the foundation method in Java.

www.ossez.com/t/java-hash…