1. Collection classes: List and set comparisons, individual sub-class comparisons (Arraylist,vector,inkedlist,hashset,treeset)
List: Deposit elements in order, elements can be repeated, allow Null is worth to exist, there are 3 main implementation classes
LinkedList: The bottom is a doubly linked list, with each node having a pointer to the previous and next node points. Because of this feature, he is characterized by the addition and deletion of fast, also because his stored elements in memory are not contiguous storage, lookup efficiency is less than ArrayList
ArrayList: The underlying is a dynamic array, so his search is more efficient, but because the increased deletion involves the large-scale movement of elements, the increase and deletion efficiency will be lower than listedlist. One situation is that the end node of the array is added and deleted, because it does not involve moving the array elements, the efficiency is very high, down is that he is thread unsafe
In a multithreaded environment, one thread reads, another thread writes, and Concurrentmodifitionexception appears, causing the fail-fast mechanism.
Vector: He is thread-safe, in some ways above add synchronized, to ensure thread safety, but in the multi-threaded environment, due to congestion, so the efficiency is particularly low, in a multithreaded environment, to ensure efficiency can use the thread-safe list, Like Copyonarraylist.
Set: His most significant feature is that the set elements can not be repeated, with the hashcode and Equals method to re-judge. The elements are unordered because of the hash hash structure. Null values are not allowed. He has two main implementation classes.
Hashset: He accesses it according to the hash structure. So the storage efficiency is higher. (Personal opinion: He was in fact a package for HashMap.) A hashmap is passed in the constructor)
TreeSet: He internally either implements an comparator interface to the element, sorts the elements, and his implementation is based on TreeMap.
The bottom of the 2.HASHMAP implementation, and then ask Concurrenthashmap the underlying implementation
The underlying implementation principle of HashMap : First, his underlying data structure is based on the array + linked list implementation. This is hashmap1.7 and its previous introduction of the red-black tree in 1.8.
The focus is on his access to the array: First, he has a key-value pair. When the Get method is called: He first calculates the value of the key's hashcode, and then calls the hash function method to perform a high-level operation, obtains his HA value, makes a call to the Indexfor method, determines his specific position in the array, and the concrete implementation is the hash value with the upper array length-1. In the put, he will first judge, in the computed array subscript position there is no element exists, if not inserted, if any, that is, a hash conflict, then the array node as the head node, the following elements are traversed. If the key is the same, then the value of the corresponding key is modified to return the old value. If the traversal result does not have the same key, then the header will be inserted.
This means that in the process of inserting may be designed to expand the problem, that is, the hashmap actual stored element value is greater than the maximum threshold value, the maximum threshold = HashMap initial capacity * load factor. In general, the initial capacity is 16, the load factor is 0.75, of course this is can become. When this happens, the original array length is changed to twice times the size of the member. Then we re-hash the key and put it in the correct position.
1.8 Several optimizations are made on this basis. 1. When inserting element >8, the following list is converted into a red-black tree.
Down is in the high-level operation, in the time efficiency performance considerations, 1.7 in the calculation of the hash value is very complex, three times or operation, in 1.8, only one time XOR operation, an XOR hash value or hash unsigned right shift 16 bits.
is in the expansion of time, 1.7 to recalculate the hash value of the element, 1.8 to optimize, the result of optimization is such as the original number of elements in the array table is 5 of the position, then the expansion, these elements either in the original position, or in the original position + the original array length position, greatly improve the efficiency of the expansion. There's a couple of details down there. I remember it was like a onlyif or something, I understand that this is equivalent to a flag bit, if the same key is present at the time of insertion, if the flag bit is ture, it can be overwritten if false.
Besides, his get method. When he makes a get, he still carries out a hash operation and a high-indexfor modulo operation, which determines the position of the subscript in the array and reads it.
3. How to implement HashMap sequential storage: You can refer to the underlying implementation of Linkedhashmap
He has a subclass, which is linkedhashmap, which guarantees the order in which elements are inserted when they are inserted.
the difference between 4.HashTable and Concurrenthashmap
First, Hashtable and con are all thread-safe collections.
The biggest difference between the two is that in the operation of the Hashtable, will lock the entire table, and con introduced the concept of segmented lock, each segment segment is a reentrant lock, the introduction of his meaning is that a thread to this section of the time when the read operation, Other threads can read but cannot write, and other threads cannot read or write while one thread is writing.
That is, each segment is equivalent to a hahtable, the thread does not lock the whole table, will only lock the corresponding segment, improve the concurrency.
That's the main difference.
5. The difference between String,stringbuffer and StringBuilder
String is final decorated, so he is thread-safe, but when the string is added and removed, he will not be modified on the basis of the original string, but in a new string, the original string string exists with the JVM memory, This can cause a lot of wasted space.
The appearance of StringBuffer and StringBuilder is to solve this problem, the modification of the string is carried out on the original string, and will not be reborn into a string.
The difference between Sringbuffer and Sringbuilder is that StringBuffer is safe in multithreaded environments, and Stringbuilfder is thread insecure by adding synchronized to the method. So his efficiency avoidance stringbuffer high.
I've seen a bit of source code means that in the implementation of Stirngbuffer security, his implementation is still using the StringBuilder to operate.
Application: In a single-threaded environment, the use of STRINGBUILDERXIAOL will be higher. In multi-threaded environment, with StringBuffer security will be higher, the corresponding efficiency will be under.
Java Face question Basics (collection)