Map set


1. Features of map set:

(1) One pair of elements is stored at a time, that is, key value pair (vlaue);

(2) The data types of key and value can be the same or different;

(3) Key cannot be duplicate. If the key is the same, the value stored later will overwrite the previous value;

2. Implementation class of map interface:

(1) HashMap: an unordered collection. The order of storing and fetching elements may be inconsistent. Null values and keys are allowed to be stored;

Underlying data structure: hash table

(2) LinkedHashMap: an ordered set, in which elements are stored and taken out;

Underlying data structure: hash table + linked list

3. Hash table data structure (linked list, array combination)

(1) Features: fast storage and retrieval; unsafe thread, fast operation; disorder, no index; no duplicate elements stored.

(2) Implementation principle: (vertical represents array, in Java, the initial length of the array is 16 by default, and the loading factor is 0.75 by default, that is, when the length of the array reaches 16 * 0.75 = 12, the array expands to twice the original size (the array is in the hash); horizontal represents the linked list)


(3) Stored procedure: when a key value pair is added, the hash value of the key is calculated first. The elements with the same hash value are placed in an array element (example: a [0]) to form a linked list, that is, an array element stores a linked list with the same hash value.

When the length of the list is too long, the list will turn into a red black tree, which greatly improves the efficiency of searching.

4. Method of HashMap:

(1) Creating objects: Map map=new HashMap();

Example: Map map=new HashMap();

(2) Storage element: V map.put (k, V); in general, null is returned; when duplicate key is stored, the value before overwrite is returned;

(3) Take out the element: V map. Get (k); get the value through the key. If there is no such key in the collection, return null;

(4) Remove element: V map.remove (k); remove the key value pair in the collection and return the value before removal;

(5) Map set traversal:

Method 1: get value with key

Call the method keyset of the map collection, store all the keys in the set collection, use the iterator or for enhancement to traverse the set collection, obtain all the elements in the set collection (i.e. the key in the map), call the get method, and obtain the value through the key

Example: Set set=map.keySet();

Iterator it=set.iterator();


Key type key = it. Next();

Value type value = map. Get (key);


Method 2: using key value pair mapping relationship

Call the method entryset of the map set, store the mapping relation objects in the set set set, use the iterator or enhanced for to traverse the set set, and obtain all elements in the set set set (mapping relation object map. Entry),

Get the corresponding key and value through the method getkey() getvalue() of the mapping relationship object

Example: Set

> set=map.entrySet();

for(Map.Entry entry:set){

K type key = entry. Getkey();

V type value = entry. Getvalue();


5. Hashtable: the interface implementation class of map. The method is the same as that of HashMap,

Different points: hashtable: thread safety, slow running speed;

HashMap: unsafe thread, fast running.

Recommended Today

The way of nonlinear optimization

Mathematical knowledge 1、 Nonlinear functionLinear function is another name of a function of first degree, then nonlinear function means that the function image is not a function of a straight line.Nonlinear functions include exponential function, power function, logarithmic function, polynomial function and so on. 2、 Taylor expansion1. Taylor formula:Taylor’s formula is to add a_ The […]