collection(list,set,map)、HashMap

collection What's inside a subclass? (List and is set to achieve a collection interface.) 

List:

1 may allow a duplicate object (reusable ordered set).
2. plurality null elements may be inserted.
3. The common implementation class of ArrayList, LinkedList and Vector. ArrayList most popular, which provides free access to use the index, while LinkedList is often more appropriate to add or remove elements from the List of occasions.
----------------

Set:

1. does not allow duplicate objects (non-repeatable, random collection).
2 allows a null element
3.Set interface to several of the most popular is the implementation class HashSet, LinkedHashSet and TreeSet. The most popular implementation is based HashSet HashMap; TreeSet SortedSet also implements the interface, thus TreeSet an orderly set of ordered according to their definitions compare () and the compareTo () of. And can be reused.
----------------
the Map:

1.Map collection sub-interface or not implemented class. Map is an interface.
2. does not allow duplicate elements.
3. Map where you can have a null value freely but only one null (key) button.
4. Map the most popular of several interfaces implementation class is HashMap, LinkedHashMap, Hashtable and TreeMap. (HashMap, TreeMap most commonly
used) ----------------

List implementation class has ArrayList, Vector and LinkedList:

ArrayList and the Vector is an array of internal structure, thread-safe, in the query efficiency would be much higher. Vector is thread-safe, the performance will be slower.

LinkedList: doubly linked list is a data structure, the former will be in accordance with the number of index data when making inquiries or backward traversal, query efficiency is low, insert faster.

Set implementation class has HashSet and TreeSet:

HashSet: interior is by a hash table (actually a HashMap instance) support. Collection elements can be null, but only into a null. Unordered.

TreeSet: binary tree is realized, ordered, or sorted according to a Comparator provided when creating the set.

LinkedHashSet: storage location is determined based on the element hashCode value of the element, but it simultaneously maintain order in the list of elements. Orderly, fast query, insert slow. When the walk through the collection time, LinkedHashSet order of the elements will be added to access elements of the collection. LinkedHashSet when iteration access to all elements of Set, the performance is better than HashSet, but when you insert performance slightly inferior to HashSet.

Map implementation class has HashMap, Hashtable, TreeMap and LinkedHashMap:

Hashtable: internal storage of key-value pairs are disordered are sorted by hash algorithm, and the biggest difference is HashMap thread-safe. Key or value is not null, null to null pointer exception is thrown.

HashMap: Map interface inheritance to achieve the hash table that allows null, thread-safe. A hash table structure is actually an array of + chain; in predetermined JDK1.8: When chain length greater than 8, the length of the list is converted to red-black tree, the search efficiency is greatly improved.

TreeMap: red-black tree based (binary) data structure implemented by the sort key, default sort is ascending.

LinkedHashMap: Map ordered collection implementation class, by insertion sort, the equivalent of a stack, first put into the last out, last out.

————————————————-------------------------------------------------------------------------------------------------------------------

Hashmap data structure look like? How achieve a hashmap?

+ Is the main data structure list an array. The default length is 16.

Hashmap underlying data structure is a linked list consisting of an array +, are thread-safe, to allow the key and value is null. The underlying structure of the array is called a hash bucket, and the bucket is a node Node list, the list of the actual storage elements.

What is the underlying principle HashMap?

Hashing based on the principle of the data structure used jdk8 list array + + red-black tree. We put and get to store and retrieve objects. When we pass to a key value and put () method, to make a pair of keys hashCode () is calculated to obtain its position in the array to store the bucket Entry object. When acquiring the object to get acquired by the position of the bucket, and then () method to find the correct key by key equals object, then the object in the return value.

How is the realization of the underlying HashMap?

HashMap first hash key is obtained, by disturbing the function obtained Hash value (reduced collisions), followed by hash & (n -1), n is the length of the bit table, to obtain the array index value calculation after. If the current node element exists, by comparing the hash value are equal and the key value, the replacement is equal to, not equal to find by a zipper element method, until equal or find the next node is null.

What is the hash (hash)?

hash definition: Hash general translation hash, the hash refers to a process that is to an input of arbitrary length, by a hash algorithm, converted into a fixed-length output, called a hash value outputted.

Hash calculation rules (algorithm)?

The first step: callkey.hashCode()。

Step 2: 16-bit exclusive hashCode high value or a low 16-bit (16-bit right shift with the original hashCode then exclusive OR value, i.e. their upper half and the lower half region XOR, this low doping after mixing the upper part of the information):

    return (key==null)?0:h=key.hashCode()^h>>>16;

The third step: a modulo operation, the modulo operation: h&(length-1) wherein length is the length of the array table. h & (length-1) is equivalent to h% length.

Hashmap how to obtain a hash subscript position?

1, the hashes the hash ();
int key.hashCode H = ();
2, a binary number is obtained here: for example:
H = key.hashCode (); // get a 32-bit binary number
** H: 0000 0000 0000 1101 0001 0101 0101 1111 **
H >>> 16: 0000 0000 0000 0000 0000 0000 0000 1101

3, // 16-bit right shift
the hash H ^ = (H >>> 16): 0000 0000 0000 1101 0001 0101 0101 0010
. 4, converted into binary 16-bit right shift, and >>> hahsCode values bitwise XOR.

5, then index = (16-1) && hash; index: 0000 0000 0000 0000 0000 0000 0000 0010
after conversion to binary index index = 2 is stored in the array position.

Why HashMap length is a multiple of 2?

In the operation flow HashMap, first hash algorithm will be a key index, the index value is the corresponding index Hash bucket array. To obtain the index value must be the number of disturbances modulo operation with the array length. That hash% n (n is the length hashmap), and because &% faster than the operation. If n is a multiple of 2, it can be converted to &%, the result is hash & (n-1). So this explains why HashMap length is a multiple of 2.

What hash collision is how to solve?

Two different original value similar results were obtained after hashing, so that a hash collision.
Solution: open-addressable, chain address law.

Open Addressing Method: The principle is the same position in a string of the hash value list data stored in a HashMap, the plurality of the original hash values differ same result stored data list.
Chain address method: When an address conflict, according to some other method of detecting the storage unit continues the hash table until it finds an empty positions so far.
ConCurrentHashMap and HashTable difference?

Both are thread-safe, but hashTable locked the entire map, and inefficient. ConcurrentHashMap using the cas + synchronized mechanism, does not lock the entire map, but the array position corresponding to the lock table list.
Generally do not use hashTable, recommended ConCurrentHashMap.

HashMap talk about what time of the need for expansion, expansion resize () is how to achieve?

Recalling a scene:

1. Initialize Array table

2. When the size of the array table reaches threshold i.e. ++ size> load factor * when the capacity, but also in function putVal

Implementation :( go into detail)

1. determined by the capacity of the old array is greater than 0 to determine whether the initialized array

No: initialize

  • Determine whether to call no-argument constructor,
    • Is: Use the default size and threshold
    • No: Use constructor initializes capacity, of course, after the number of times the capacity of the power calculation tableSizefor 2

Is for expansion, expansion to double (at less than the maximum value), after performing the re-elements with copy operation to a new hash table

Speaking in general terms: expansion need to re-allocate a new array, a new array is twice the length of the old array, and then traverse the entire old structure, all the elements one by one to re-hash assigned to the new structure to go.

PS: visible underlying data structure uses an array, in the end because of capacity problems need to be operational expansion

Talk about how to get in hashMap is achieved?

To key the hashCode were hashing, subscript and arithmetic calculation acquire bucket position, if it can be found on top of the barrel directly returned, otherwise find in the tree or linked list traversal to find, if there are hash conflict, using the equals method to traverse the list to find the node.

Talk about the HashMap hash function is how to achieve? There are ways to achieve what hash function?

hashCode to do hash key operation, 16-bit high and exclusive ORing

There are middle-square method, except I stay, pseudo-random number method

Why not the key as a hash value but with 16-bit high-XOR operation?

Since operation is determined by the location of the array, only the last four significant, designer key hash value 16 is high so that the exclusive ORing & doing arithmetic determining the insertion position of the array, this time the actual low It is a combination of high and low, increasing the randomness, reduce the number of hash collisions.

Why 16? Why must be a power of 2? If the input value is not a power of 2 such as 10 what will happen?

https://blog.csdn.net/sidihuo/article/details/78489820

https://blog.csdn.net/eaphyy/article/details/84386313

1. For a uniform distribution of data, reducing the hash collision. Because the array to determine the position of the bit operation is used, if the data is not a power of 2 it will increase the number of waste and an array of space hash collision. (PS: In fact, without considering the efficiency, can I ask do not place the power of computing do not have the required length is 2)

2. If the input data is a power of 2, affirmative HashMap obtained through a shift operation by the OR operation, and the number is a power of 2, and recently from the digital number

Talk about what happens when two objects are equal hashCode?

It will produce a hash collision, if the key value is the same as the old value is replaced, or link to the back of the list, the list is longer than the threshold into red-black tree stored on 8

If hashcode two keys are the same, how do you get the value of the object?

Same HashCode, obtain the value of the object by comparing the contents equals

Guess you like

Origin www.cnblogs.com/lgg20/p/12324606.html