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 use the get method of Hashmap in java

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces how to use the get method of Hashmap in java. The introduction in the article is very detailed and has certain reference value. Interested friends must read it!

Get method of Hashmap in java

Map stores key-value pairs, that is, parameters and values are stored by set method, and then values are read by get"key" form.

For example Map map = new Hashmap();//Create a mapmap.put("key","value");//Assign String vlaues = map.get("key") to map;//Get the value system.out.println(vlaues ) with key "key" in map;//Output the result

The result of running the above code:

value;

The principle of get method in HashMap

1. First pass a key to get() method 2. Call hash(key) in get() method

If key!= null, return hash = key.hashCode()^ (h >>> 16), otherwise return hash=0

3. Call getNode(hash,key) method in get() method

Get the node of the key and return value

getNode() method

First of all, it is necessary to judge whether the Hashtable is empty and the table length is greater than 0 and the table element corresponding to the hash value is not empty. If the condition is true, it is judged whether the hash value of the node is equal to hash, and the linked list or red-black tree is traversed in turn to find key==node.key? Returns the value of the node found

// JDK public V get(Object key) { Node e; return (e = getNode(hash(key), key)) == null ? null : e.value;} final Node getNode(int hash, Object key) { Node[] tab; Node first, e; int n; K k; //Determine whether hashtable is empty, tab[ ] corresponding to key is empty if ((tab = table) != null && (n = tab.length) > 0 && (first = tab[(n - 1) & hash]) != null) { //Determine whether the hash and key of the first node are equal if (first.hash == hash && // always check first node ((k = first.key) == key || (key != null && key.equals(k)))) return first; //determine whether the next node is empty if ((e = first.next) != null) { //Determine whether it is a node of the red-black tree and traverse the search element if (first instanceof TreeNode) return ((TreeNode)first).getTreeNode(hash, key); do { if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) return e; } while ((e = e.next) != null); } } return null; } The above is "How to use the get method of Hashmap in java" All the contents of this article, thank you for reading! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

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