Home > Java > javaTutorial > body text

Detailed explanation of the usage of Map in Java

高洛峰
Release: 2017-01-19 10:40:33
Original
1397 people have browsed it

Map Introduction

An object that maps keys to values. A map cannot contain duplicate keys; each key can only be mapped to at most one value. This interface replaces the Dictionary class, which is entirely an abstract class rather than an interface.

The Map interface provides three collection views, allowing you to view the contents of a map in the form of a key set, a value set, or a key-value mapping relationship set. Mapping order is defined as the order in which an iterator returns its elements on a mapped collection view. Some mapping implementations can explicitly guarantee their order, such as the TreeMap class; other mapping implementations do not guarantee the order, such as the HashMap class.

Note: You must be careful when using mutable objects as map keys. When an object is a key in a map, the behavior of the map will be undefined if the object's value is changed in a way that affects equals comparisons. A special case of this prohibition is disallowing a map to contain itself as a key. Although a map is allowed to contain itself as a value, be careful: the equals and hashCode methods are no longer well-defined on such a map.

Map interface:

Map provides key to value mapping. A Map cannot contain the same key, and each key can only map one value. The Map interface provides three kinds of set views. The content of the Map can be regarded as a set of key sets, a set of value sets, or a set of key-value mappings.​

Hashtable class ​

Hashtable inherits the Map interface and implements a hash table of key-value mapping. Any non-null object can be used as key or value.​​

To add data, use put(key, value), and to remove data, use get(key). The time cost of these two basic operations is constant. Hashtable adjusts performance through two parameters: initial capacity and load factor. Usually the default load factor 0.75 achieves a better balance of time and space. Increasing the load factor can save space but the corresponding search time will increase, which will affect operations like get and put.

A simple example of using Hashtable is as follows. Put 1, 2, and 3 into the Hashtable. Their keys are "one", "two", and "three" respectively:

Hashtable numbers = new Hashtable(); 
numbers.put(“one”, new Integer(1)); 
numbers.put(“two”, new Integer(2)); 
numbers.put(“three”, new Integer(3));
Copy after login

To take out a number, such as 2, use the corresponding key:

Integer n = (Integer)numbers.get(“two”); 
System.out.println(“two = ” + n);
Copy after login

Since the object used as the key will be determined by calculating its hash function to correspond to it The position of the value, so any object used as a key must implement the hashCode and equals methods. The hashCode and equals methods inherit from the root class Object. If you use a custom class as a key, be very careful. According to the definition of the hash function, if the two objects are the same, that is, obj1.equals(obj2)=true, then Their hashCode must be the same, but if two objects are different, their hashCode is not necessarily different. If the hashCode of two different objects is the same, this phenomenon is called a conflict. The conflict will cause the time overhead of operating the hash table to increase. Therefore, try to define a well-defined hashCode() method to speed up hash table operations.

If the same object has different hashCode, the operation of the hash table will have unexpected results (the expected get method returns null). To avoid this problem, you only need to remember one thing: copy at the same time equals method and hashCode method, rather than just writing one of them.

Hashtable is synchronous.

HashMap class

HashMap is similar to Hashtable, except that HashMap is asynchronous and allows null, that is, null value and null key. , but when treating HashMap as a Collection (the values() method can return a Collection), the time overhead of its iteration sub-operations is proportional to the capacity of the HashMap. Therefore, if the performance of iterative operations is very important, do not set the initial capacity of HashMap too high or the load factor too low.

WeakHashMap class

WeakHashMap is an improved HashMap, which implements a "weak reference" to the key. If a key is no longer referenced externally, then the key Can be recycled by GC.

The above is an introduction to the Java Map interface. Students who learn Java programming can refer to it.

For more detailed explanations of the usage of Map in Java and related articles, please pay attention to the PHP Chinese website!


Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template