Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand hashCode () in java

Shulou Source: shulou.com Published: 2022-06-03 03:15:22 10月17日 Update

This article mainly explains "how to understand hashCode () in java". The content of the explanation in this article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to understand hashCode () in java".

1. The function of hashCode is to get hash codes, also known as hash codes, which actually return int integers. The purpose of the hash code is to determine the index position of the object in the hash table.

2. HashCode is defined in the Object class of JDK, which means that any class in Java contains hashCode functions.

Example

Package com.tools; import java.util.ArrayList; public class HashCodeMeaning {public static void main (String [] args) {ArrayList list = new ArrayList (); int numberExist=0; / / proves that the value of hashcode is not the memory address for (int I = 0; I < 10000; iTunes +) {Object obj=new Object () If (list.contains (obj.toString () {System.out.println (obj.toString ()) + "exists in the list. "+ I); numberExist++;} else {list.add (obj.toString ());}} System.out.println (" repetition number: "+ numberExist); System.out.println (" list size: "+ list.size ()); / / prove that the memory address is different. NumberExist=0; list.clear (); for (int I = 0; I < 10000; iTunes +) {Object obj=new Object (); if (list.contains (obj)) {System.out.println (obj + "exists in the list. "+ I); numberExist++;} else {list.add (obj);}} System.out.println (" repetition number: "+ numberExist); System.out.println (" list size: "+ list.size ()) }} Thank you for your reading. The above is the content of "how to understand hashCode () in java". After the study of this article, I believe you have a deeper understanding of how to understand hashCode () in java, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Hash learning function memory content address different location function instance actual actually object that is idea situation meaning integer article more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB OPPO Reno Microsoft Linux Redmi