JAVA container-related issues (a)

HashMap related issues

1, you used HashMap it? What is HashMap? Why do you use it?

Used, HashMap is asynchronous implementation of the Map interface based on the hash table that allows null keys and null values, and HashMap relying on the design of its data structure, storage efficiency is particularly high, this is the reason I use it

2, you know works HashMap do? Do you know the working principle of HashMap get () method do?

The above two issues belong to the same answer to the question

HashMap is, the object may be acquired by the hash algorithm put (key, value) is stored by the object to the HashMap get (key) based on the HashMap. When we use the put, first of all would be key in HashMap hashCode () The hash value is calculated, it is worth to the position of the element in the array based on hash, the position will be on the list of elements stored in. When we get to use the first value of the key will HashMap hashCode () performs the hash calculation, it is worth to position this element in the array according to the hash, the element is removed from the list on this position

3, when two objects of the same hashcode what happens?

Same hashCode, described the same position on the two objects HashMap array, then each element will traverse HashMap linked list to determine whether the same key, if the key is the same, then the new value will be covered by the equals method to key the old value, and returns the old value. If not the same key, the position on the list is stored in the first chain

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

HashMap through each element in the list, and for each key hash calculation, and finally obtain the corresponding value of the object by the get method

5, if the HashMap size exceeds the load factor (load factor) defined capacity, how to do?

The default load factor is 0.75, HashMap exceeds the load factor is defined as the capacity that is more than the (HashMap size * load factor) this value, HashMap HashMap will be created for the original double the size of the array size, as its new capacity, this process is called resize or rehash

6, you know what problems exist HashMap re-adjust the size of it?

When the case of multi-threaded, possible conditions of competition. When re-adjust the size of the HashMap time, the existence of conditions of competition, if two threads are found HashMap need to resize, and they will also try to adjust the size. In the resizing process, the order of elements stored in a linked list in turn, because moving to a new location when the array, HashMap LinkedList does not take an element on the tail, but on the head, which is In order to avoid traversing the tail (tail traversing). If the conditions of competition occurs, then the cycle of death

7, we can use custom objects as keys do?

It may be, as long as it complies with the rules define equals () and hashCode () method, and when the object is inserted into the Map will not be changed. If the custom object is immutable, it has met the conditions as a key, because when it created has not changed.

HashSet and HashMap difference

HashMap implements Map interface 

HashSet implements Set Interface

HashMap to store key-value pairs 

HashSet only store objects

HashMap using put () method of the element into the map 

Use HashSet add () method set in the element into

HashMap key object used to calculate the hashcode value 

HashSet member objects to calculate the hashcode value

HashMap faster because it is using a unique key to get the object 

HashSet is slower than HashMap

HashTable and HashMap difference

Hashtable methods are synchronized 

The method of non-synchronous HashMap

Based Dictionary Hashtable class 

HashMap based AbstractMap, and AbstractMap-based implementation of the Map interface

Hashtable in the key and value are not allowed to null, met null, direct return NullPointerException 

HashMap of key and value are allowed to be null, null key is encountered when calling putForNullKey method for processing, while the value does not handle

The default size of the Hashtable hash array 11, the expansion method is old * 2 + 1 

The default size of the HashMap hash array 16, and must be a power of two

LinkedHashMap orderliness

LinkedHashMap underlying hash table with a doubly linked list to hold all the elements, it maintains a doubly linked list of all entries to run (if the school had a doubly linked list of students will better understand its source code), this list defines the iteration order this iterative sequence can be inserted into the sequence or order of access 

1. Press list of the inserted sequences: LinkedHashMap after calling the get method and the same as the input and output sequence, which is inserted according to the order list, the default is sorted in order of insertion

2. list the order of access: After LinkedHashMap call the get method, the elements of the visit will be moved to the tail of the list, you can continue to access the form sorted in order to access the list. Simply put, to sort (similar LRU algorithm) as the element least recently accessed

LinkedHashMap and HashMap difference

LinkedHashMap orderly, insertion order and access order 

HashMap disorder

Internal LinkedHashMap maintains a doubly linked list of all entries to run 

Internal HashMap maintains a single list

What is ArrayList

ArrayList can be understood as a dynamic array, its capacity to dynamically increase the size of the array capacity is used to store the list of elements, with the elements are added ArrayList, automatically increase its capacity 

ArrayList permits all elements, including null, including 

ArrayList is non-synchronous implementation of the List interface 

ArrayList is ordered

ArrayList implements List interface, using the underlying array holds all the elements, which operation is an operation on an array of substantially

ArrayList AbstractList inherited abstract class, it's an array queue, provided relevant to add, delete, modify, traversal function

RandmoAccess ArrayList implements an interface that provides random access, RandmoAccess is being used in java implementation of List, List provides quick access to functions, we can quickly get the element object by number of elements, which is fast random access

ArrayList implements Cloneable interface, i.e. covering function clone (), can be cloned 

ArrayList implements java.io.Serializable interface, support serialization means ArrayList

What is LinkedList

Based on non-synchronized LinkedList List interface of the list 

LinkedList allows all the elements, including null, including 

LinkedList is ordered 

LinkedList is a fail-fast

LinkedList and ArrayList difference

The bottom layer is a doubly linked list LinkedList 

ArrayList underlying array variable

LinkedList does not allow random access, that is, low query efficiency 

ArrayList allow random access, that is, high query efficiency

LinkedList fast insertion and deletion efficiency 

Low insertion and deletion efficiency ArrayList

For two random access, get, and set, ArrayList LinkedList superior, to move the pointer because LinkedList 

Two methods to add and delete, add and remove, LinedList comparative advantage, because you want to move data ArrayList

What is ConcurrentHashMap

ConcurrentHashMap synchronous implementation based on the Map interface and double linked list of arrays 

ConcurrentHashMap key elements are unique, value values ​​may be repeated 

ConcurrentHashMap not permit null values ​​and the null key 

ConcurrentHashMap are unordered

Why ConcurrentHashMap

We all know that HashMap is not thread safe, when we have only one thread in the use of HashMap, naturally there will be no problem, but if it comes to multiple threads, and have read there is the process of writing, HashMap will fail-fast. To solve the problem HashMap synchronized, our solutions have

Hashtable 

Collections.synchronizedMap(hashMap) 

Both methods are basically with synchronization lock on the entire hash table structure, so that during the lock on a table, you need to wait for another thread, no doubt, performance is not high, so we introduce ConcurrentHashMap, but both synchronous multi-threaded access

ConcurrentHashMap data structure

ConcurrentHashMap Segment data structure array is a data structure for HashEntry Segment array, while our HashEntry stored key-value pair may constitute the list. It can be simply understood as the array is filled with HashMap

From the above we can understand the structure, ConcurrentHashMap a positioning element of the process requires two operations Hash, Hash first locate the Segment, second Hash positioned to head the list of the element, and therefore, this kind of structure Hash is a side effect of the process is longer than ordinary HashMap, but the benefits it is time to write the elements can only be locked Segment where you can, does not affect other Segment. It is precisely because of its internal structure and mechanism, ConcurrentHashMap performance than HashMap Hashtable and after synchronization package on the performance of concurrent access to improve a lot. In the ideal situation, ConcurrentHashMap can support 16 concurrent threads of execution write operation (if the concurrency level set to 16), and any number of threads read

What is Vector

Vector is synchronized with List-based interface to achieve a variable array of 

Vector is ordered 

Vector key allows null and a null value 

Vector has not recommended a

Vector List interface implements the underlying storage array using all the elements, which operation is an operation on an array of substantially 

Vector AbstractList inherited abstract class, it's an array queue, provided relevant to add, delete, modify, traversal function 

Vector realized RandmoAccess interface, that provides random access, RandmoAccess is being used in java implementation of List, List provides quick access to functions, we can quickly get the element object by number of elements, which is fast random access 

Vector implements Cloneable interface, i.e. covering function clone (), can be cloned 

Vector implements java.io.Serializable interface, support serialization means ArrayList

Vector and ArrayList difference

Vector synchronized, thread-safe 

ArrayList asynchronous thread safe

Vector overhead required to maintain synchronization lock, slow performance 

ArrayList fast performance

Vector can be used Iterator, foreach, Enumeration output 

Only use ArrayList Iterator, foreach output

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_33391981/article/details/91311567