Home Java javaTutorial Detailed explanation and simple examples of Java hash storage

Detailed explanation and simple examples of Java hash storage

Feb 03, 2017 pm 01:12 PM

Java Hash Storage

The data structures of hash storage in Java mainly refer to HashSet, HashMap, LinkedHashSet, LinkedHashMap and HashTable, etc. To understand the hash storage mechanism in Java, we must first understand two methods: equals() and hashCode(). Regarding the equals() method and its difference from the "==" relational operator, we have explained it in another article. As for hashCode(), it is a method defined in the Object class:

public native int hashCode();
Copy after login

This is a local method that returns an int value and is not implemented in the Object class. This method is mainly used in data structures that use hashing, and works normally with hash-based collections. For example, when inserting an object into a container (we assume it is a HashMap), how to determine whether the object already exists in the container? Where is the target? Since there may be thousands of elements in the container, using the equals() method to compare them sequentially is very inefficient. The value of hashing is speed, it saves the key somewhere so it can be found quickly. The fastest data structure to store a set of elements is an array, so use it to store key information (note that it is the key information, not the key itself). But because the array cannot adjust its capacity, there is a problem: we want to save an uncertain number of values ​​in the Map, but what should we do if the number of keys is limited by the capacity of the array?

The answer is: the array does not save the key itself, but generates a number through the key object and uses it as the subscript of the array. This number is the hash code (hashcode), which is defined in Object , and may be generated by the hashCode() method overridden by your class. To solve the problem of fixed array capacity, different keys can produce the same subscript, a phenomenon called a conflict. Therefore, the process of querying a value in the container is: first calculate the hash code of the object to be inserted through hashCode(), and then use the hash code to query the array. Conflicts are often handled through external links, that is, the array does not directly store the value, but a list of values, and then a linear query is performed on the values ​​in the list. This part of the query will naturally be slower. However, if the hash function is good enough, there will be fewer values ​​at each position in the array. Therefore, the hashing mechanism can quickly jump to a location in the array, comparing only a few elements. This is why HashMap is so fast. We can realize it through the HashMap.put() method:

public V put(K key, V value) {
  if (table == EMPTY_TABLE) {
   inflateTable(threshold);
  }
  if (key == null)
   return putForNullKey(value);
  int hash = hash(key);
  int i = indexFor(hash, table.length);
  for (Entry<K,V> e = table[i]; e != null; e = e.next) {
   Object k;
   if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
    V oldValue = e.value;
    e.value = value;
    e.recordAccess(this);
    return oldValue;
   }
  }
 
  modCount++;
  addEntry(hash, key, value, i);
  return null;
 }
Copy after login

The main idea is: the key is not When empty, the hash code hash is obtained according to the key object, and then the subscript i of the array is obtained through the hash code. Iterate through the list represented by table[i] and determine whether the key exists through equals(). If it exists, update the old value with the new value and return the old value; otherwise, add the new key-value pair to HashMap. It can be seen from here that the hashCode method exists to reduce the number of calls to the equals method, thereby improving program efficiency.

Here we need to note: hashCode() does not always need to be able to return a unique identification code, but the equals() method must strictly determine whether two objects are the same.

Thank you for reading, I hope it can help you, thank you for your support of this site!

For more detailed explanations and simple examples of Java hash storage, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Is the company's security software causing the application to fail to run? How to troubleshoot and solve it? Apr 19, 2025 pm 04:51 PM

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

How to elegantly obtain entity class variable names to build database query conditions? How to elegantly obtain entity class variable names to build database query conditions? Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to simplify field mapping issues in system docking using MapStruct? How to simplify field mapping issues in system docking using MapStruct? Apr 19, 2025 pm 06:21 PM

Field mapping processing in system docking often encounters a difficult problem when performing system docking: how to effectively map the interface fields of system A...

How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log? Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to safely convert Java objects to arrays? How to safely convert Java objects to arrays? Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? How to elegantly get entity class variable name building query conditions when using TKMyBatis for database query? Apr 19, 2025 pm 09:51 PM

When using TKMyBatis for database queries, how to gracefully get entity class variable names to build query conditions is a common problem. This article will pin...

How do I convert names to numbers to implement sorting and maintain consistency in groups? How do I convert names to numbers to implement sorting and maintain consistency in groups? Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products? Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

See all articles