A collection of separate collection of interview questions List

Vector and ArrayList and LinkedList differences and connections, as well as the respective application scenarios
thread safety:
Vector: not specified when creating Vector capacity, the default size is 10 and the underlying array-based implementation, thread-safe, are based on synchronized synchronization methods plus lock

 

Add Method

 

 

 

Query methods

 

 

 

 

 

 

 

 

 


ArrayList: Based on the underlying array, thread-safe, high efficiency query and modify, add and delete but low efficiency

Add Method

 

 

 Query method:

 

 

 

 

 

 

 

 

 

LinkedList: underlying doubly linked list structure, thread-safe, low efficiency of query and modify, add and delete but high efficiency

Add Method

 

 

 Query methods

 

 

 

 

 

 

 

 

 


Usage scenarios:
1.Vector rarely use
2. If you need a large number of additions and deletions can be selected LinkedList
3. If you need a large number of query and modify the ArrayList can choose


if you want to ensure ArraList thread-safe, there are several ways?
Table 1 himself write a ArrayList collection class, according to the service in general, add / set / remove lock
2 using List <Object> list = Collections.synchronizedList ( new ArrayList <> ()); // use of synchronized locking
3 new . CopyOnWriteArrayList <> () add ( ""); // using ReentrantLock locking

understand CopyOnWriteArrayList bottom? , CopyOnWriteArrayList and Collections.synchronizedList What is the difference
1 CopyOnWriteArrayList underlying implementation:
CopyOnWriteArrayList modification in the implementation of the operation, will copy a new array of data, costly, and after modifying the original collection point to the new collection to complete the operation
using ReentrantLock guarantee a collection of security in a multithreaded environment

Boolean the Add public (E E) { 
	Final Lock of ReentrantLock = this.lock; // Get a lock 
	lock.lock (); // locking 
	the try { 
	  Object [] = getArray Elements (); // get the current array data , to Elements 
	  int len = elements.length; // record the current length of the array 
	  Object [] newElements = Arrays.copyOf (elements , len + 1); // copy a new array 
	  newElements [len] = e; // will among the new data into the array 
	  setArray (newElements); // pointer to the current array new data 
	  return to true; 
	} {the finally 
	  lock.unlock (); // release lock 
	} 
}

  

CopyOnWriteArrayList application scenarios: For a read operation is much greater than the write operation scenario (not get locked underlying reading, direct access)

2 plus the Collections.synchronizedList almost all the underlying methods synchronized lock
Scene: write better performance than CopyOnWriteArrayList but read performance than CopyOnWriteArrayList


4 CopyOnWriteArrayList idea is kind of how, what are the disadvantages?
Design: separate read and write, eventually consistent
Cons: memory usage, due to the copy-on-write memory, there will be two space objects, if the object is large and prone to YongGC FullGC


5 ArrayList expansion mechanism talk about is how
1 .JDK1.8 previous default size is 10, jdk1.7 expansion similar to 1.8, jdk1.6 1.5 times +1

 2.ArrayList set size, if not specified when created, it defaults to 0, when the set size has been specified, then the initial value of the specified
 time when the first added data, the expansion is set size 10, each of second and subsequent times according int oldCapacity = elementData.length; newCapacity = oldCapacity + (oldCapacity >> 1), i.e. 1.5 times the expansion

 

Go ensureCapacityInternal () method to create ArrayList size specified when the pass in the past, if you do not specify the size 0

 

The default initial capacity 10

 

 

 

If the current ArrayList judge entered judgment is empty, the minCapacity assignment, Math.max () and the initial judgment specified that big, put it assigned to minCapacity

Then go ensureExplicitCapacity () method

 

 

Determining whether the number of passed in ArrayList greater than the length of the current, if greater than the expansion proceeds, take Grow () method and pass over minCapacity

 

 

 

 

The length of the collection assigned to oldCapacity (old capacity)

newCapacity (new capacity) equals the value of oldCapacity + (0.5 * The values ​​oldCapacity)

Analyzing newCapacity-minCapacity is less than zero, it will be less than if the values ​​assigned to newCapacity minCapacity

Analyzing newCapacity-MAX_ARRAY_SIZE (set maximum capacity) is greater than zero, will be greater than if hugeCapacity () returns the value assigned to newCapacity

A collection of original and re-copied to the capacity assigned in the good new set of assignments assigned to the original collection

 

 

 

The minCapacity make a judgment if less than zero, it is reported out of memory error

Determining whether minCapacity set greater than the maximum capacity, if more than the maximum value will be returned in the past Integer type, is less than the maximum capacity of the collection set will return past the maximum capacity

 

 

 

 

Guess you like

Origin www.cnblogs.com/wishsaber/p/12490742.html