In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Java how to use the Map collection class, I believe that many inexperienced people are helpless about this, this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
The longest-used collection classes in Java are List and Map, and specific implementations of List include ArrayList and Vector, which are variable-sized lists that are more suitable for building, storing, and manipulating lists of object elements of any type. List is useful when accessing elements by numerical index.
Map provides a more general way to store elements, and the Map collection class stores pairs of elements (called "keys" and "values"), where each key maps to a value. Conceptually, a List can be thought of as a Map with numeric keys. In fact, except List and Map are defined in java.util, there is no direct connection between the two.
There are many predefined Map classes in Java core classes. Before describing the specific implementation, we should first understand the Map interface itself so that we can understand what all implementations have in common. Map interface definition can be divided into four types of methods, the two most common methods equals(Object o) is to compare the equivalence of the specified object and this Map, and hashCode() returns the hash code of this Map, you can override these two methods of this Object to correctly compare the equivalence of Map objects.
Map defines several transformation methods for inserting and deleting elements, clear() removes all data from the Map, remove(Object key) removes keys and associated values from the Map, put(Object key, Object value) associates a specified value with a specified key, putAll(Map t) copies all data from the specified Map to this map. Note: putAll is usually more efficient without having to use a large number of put() calls, and putAll() exists to iterate over the Map elements passed in addition to the algorithm performed by the user iterating over each key-value pair added to the Map. putAll() resizes the Map before adding elements. If Map is not resized, putAll() may be more efficient than expected.
Java comes with a variety of Map classes, which can be classified into three types:
1)Generic Map is used to manage mappings in applications, usually implemented in java.util packages
HashMap is a hash table based on the "zipper method" implementation. The bottom layer is realized by "array + linked list."
Hashtable A hash table based on a zipper implementation.
Properties inherits Map class
LinkedHashMap is a subclass of HashMap that stores the order in which records are inserted
IdentityHashMap implements the Map interface, using reference equality instead of object equality when comparing keys (and values).
TreeMap ordered hash table, SortedMap interface, the bottom layer through the red and black tree implementation.
WeakHashMap A hash table based on a "zipper method" implementation.
ConcurrentHashMap adopts the design of segmented locks. There is only a race relationship within the same segment, and there is no lock competition between different segmented locks.
2)Private Maps usually don't have to create such Maps themselves, but access them through some other class
java.util.jar.Attributes
javax.print.attribute.standard.PrinterStateReasons
java.security.Provider
java.awt.RenderingHints
javax.swing.UIDefaults
3)Abstract classes to help implement your Map class
AbstractMap
This class is designed to reduce the task of implementing the Map interface, and some simple generic methods do not require the Map itself to be implemented.
The two most important keywords are transient and volatile, and the source code is as follows:
Key word explanation:
transient is a variable modifier that indicates that the field is not part of the persistent state of the object and is not stored, such as when serializing the object.
Volatile is also a variable modifier and can only be used to modify variables. Volatile-modified member variables force the rereading of their values from shared memory each time they are accessed by a thread. Also, when a member variable changes, the thread is forced to write the changed value back to shared memory. This way, at any time, two different threads always see the same value for a member variable.
Here is an explanation of Java's memory mechanism:
Java uses one main memory to hold the current value of variables, while each thread has its own working memory. When a thread accesses a variable, it copies the value of the variable into its own working memory. In this way, when the thread operates on the variable in its own working memory, the value of the variable copied in working memory is different from the value of the variable in main memory.
The Java language specification states that, for optimal speed, threads are allowed to keep private copies of shared member variables and compare them to their original values only when the thread enters or leaves a synchronized block of code.
Thus, when multiple threads interact with an object at the same time, care must be taken to ensure that threads receive changes in shared member variables in a timely manner. The volatile keyword is a reminder to the VM that you cannot keep a private copy of this member variable, but should interact directly with shared member variables.
Usage suggestion: Use volatile on member variables accessed by two or more threads. Do not use when the variable you want to access is already in a synchronized block of code or is a constant. Because using volatile masks the necessary code optimizations in VM, it is inefficient, so be sure to use this keyword only when necessary.
An easy way to get the best Map performance is to declare all Map variables as Map instead of implementing them, see Example:
Map cMap = new HashMap(); //OK
HashMap cMap = new HashMap(); //Difference
This makes it very easy to replace any particular Map instance with just one line of code.
Because HashMap plays an important role in Java development, this article focuses on analyzing the important attributes and optimizations of HashMap.
Important properties of HashMap
HashMap Optimization
If the inner array of the hash map contains only one element, all items are mapped to this array position, resulting in a longer linked list. This is inefficient because our updates and accesses use a linear search of the linked list, which is much slower than if each array index in the Map contained only one object. The time to access or update a linked list is linearly related to the size of the list, whereas asking or updating a single element of an array using a hash function is independent of array size-O(n) for the former and O(1) for the latter in terms of asymptotic properties (Big-O notation). So it makes sense to use a larger array rather than having too many items clustered in too few array positions.
Resizing Map Implementation
In hashing terminology, each location in the inner array is called a bucket, and the number of buckets available (i.e., the size of the inner array) is called capacity. For the Map object to handle any number of items efficiently, the Map implementation can resize itself. But resizing is expensive. Resizing requires reinserting all elements into the new array, because different array sizes mean that objects now map to different index values. Keys that previously collided may no longer collide, while other keys that previously did not collide may now collide. This clearly shows that if you make Map large enough, you can reduce or even eliminate the need for resizing, which is likely to significantly improve speed.
After reading the above, do you know how to use Map collection class in Java? If you still want to learn more skills or want to know more related content, welcome to pay attention to the industry information channel, thank you for reading!
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.