Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Java from Map to HashMap

2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to realize Java from Map to HashMap". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to realize Java from Map to HashMap".

1. Map

1.1 Map interface

In Java, Map provides key-value mapping. The mapping cannot contain duplicate keys, and each key can be mapped to only one value.

Based on Map key-value mapping, java.util provides HashMap (most commonly used), TreeMap, Hashtble, LinkedHashMap and other data structures.

The main features of several derived Map:

HashMap: the most commonly used data structure. The mapping relationship between keys and values is realized by the Hash function. When the traversing key is unordered

TreeMap: a data structure built with a red-black tree. Because of the principle of the red-black tree, key can be sorted naturally, so the key traversal of TreeMap is arranged in natural order (ascending order) by default.

LinkedHashMap: saves the order in which to insert. The records obtained by traversal are in the order of insertion.

1.2 Hash hash function

Hash (hash function) is a hash algorithm that converts input of arbitrary length into output of fixed length. The return value of the Hash function is also known as the hash value hash code digest or hash. The effect of Hash is shown in the following figure:

The Hash function can achieve a better balance in time and space by selecting the appropriate function.

Two ways to solve Hash: zipper method and linear detection method

1.3 realization of key-value relationship

Interface Entry

Implementation based on linked list in HashMap

Static class Node implements Map.Entry {final int hash; final K key; V value; Node next; Node (int hash, K key, V value, Node next) {this.hash = hash; this.key = key; this.value = value; this.next = next;}

To implement in a tree way:

Static final class TreeNode extends LinkedHashMap.Entry {TreeNode parent; / / red-black tree links TreeNode left; TreeNode right; TreeNode prev; / / needed to unlink next upon deletion boolean red; TreeNode (int hash, K key, V val, Node next) {super (hash, key, val, next);}

1.4 API agreed upon by Map

1.4.1 basic API agreed upon in Map

Basic additions, deletions, modifications and searches:

Int size (); / return size boolean isEmpty (); / / whether it is empty boolean containsKey (Object key); / / whether it contains a key boolean containsValue (Object value); / / whether it contains a value V get (Object key); / / gets the value V put (K key, V value) for a key; / / stored data V remove (Object key); / / removes a key void putAll (Map)

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report