In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to use the Map collection in Java, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
An overview of the Map collection
Overview: interface Map where K is the type of key, the key is unique, do not repeat. V is the type of value and can be repeated. And each key can map up to one value. Note that if there are two identical keys, the current value will be replaced with the previous value.
How to create: create objects in a polymorphic form.
Features:
Key-value pair mapping relationship
A key corresponds to a value.
Keys cannot be repeated, values can be repeated
Element access disorder
Common methods:
(put (KQuery V) associates the specified value with the specified j key in the mapping for addition, return type V)
To get the function:
Where: KeySet () returns as type Set, so the key is unique and does not repeat.
Traversal mode:
The first kind:
By getting the collection of keys, and then using the get method to get the corresponding value.
The second kind:
The implementation class of Map: HashMap
Storage method:
The Key-value key-value mapping method is used to store.
Underlying structure: the storage structure of the hash table is adopted, so the data in it is unordered but unique. (the only way to do this is to override the Hashcode and equals methods.)
TreeMap
Underlying structure: the data in the binary tree storage mode is unique and ordered and is generally arranged in ascending order (to implement the comparable interface and rewrite the compareTo method to achieve its sorting).
Collection nesting (supplementary knowledge):
A similar form is:
ArrayList array = new ArrayList ()
For the nesting of collections, the main attention is to the use of types and methods of nesting collections in the collection.
High-frequency interview questions 1. Map
HashMap: before JDK1.8 HashMap consists of array + linked list, which is the main body of HashMap, and linked list exists mainly to solve hash conflicts ("zipper method" to resolve conflicts). After JDK 1.8, there have been great changes in resolving hash conflicts. When the length of the linked list is greater than the threshold (default is 8), the linked list will be converted into a red-black tree to reduce search time.
LinkedHashMap: LinkedHashMap inherits from HashMap, so its underlying layer is still based on a zipper hash structure that consists of arrays and linked lists or red-black trees. In addition, LinkedHashMap adds a bi-directional linked list on the basis of the above structure, so that the above structure can maintain the insertion order of key-value pairs. At the same time, through the corresponding operation of the linked list, the access order related logic is realized. For more information, please see "detailed Analysis of LinkedHashMap Source Code (JDK1.8)".
HashTable: array + linked list, which is the main body of HashMap, and linked list exists mainly to solve hash conflicts.
TreeMap: red-black tree (self-balanced sorted binary tree).
2. The underlying implementation of HashMap
Before JDK1.8, HashMap consists of array + linked list ("linked list hash" is the combination of array and linked list).
After JDK1.8, there are great changes in resolving hash conflicts. When the length of the linked list is greater than the threshold (default is 8), the linked list is converted into a red-black tree to reduce the search time, so it is composed of array + linked list + red-black tree.
These are all the contents of the article "how to use Map collections in Java". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.