Java collections and summarized a set of selection

Collection

1. List

  • ArrayList: Object array, is an array queue, is equivalent to a dynamic array. It is implemented by an array of high-efficiency random access, random insertion, deletion random low efficiency.
  • LinkedList: is a doubly linked list (for the circular list before JDK1.6, JDK1.7 canceled cycle). It can also be used as a stack, queue, or deque operate. LinkedList random access efficiency is low, but the random insertion, deletion random high efficiency.
  • Vector: Object array, a vector queue, and ArrayList, as it is a dynamic array, is implemented by the array. But ArrayList is non-thread-safe, but Vector is thread-safe.
  • Stack is the stack, it is inherited from Vector. Its characteristics are: last-out (FILO, First In Last Out).

 

2. Set

  • HashSet (random, unique): a hash table structure-based implementation, the underlying HashMap employed to hold the elements, the main use of HashMap key elements to store, calculate hashCode inserted element acquires the position of the element in the collection;
  • LinkedHashSet: LinkedHashSet inheritance and HashSet, and its interior is achieved through LinkedHashMap. Somewhat similar to what we said before the inside is the same LinkedHashMap based Hashmap achieve, but there are still a little bit different.
  • TreeSet (ordered only): red-black tree (self-balancing binary tree sorting), each element is a node in the tree, the insertion elements are sorted

Map

  • HashMap: Before JDK1.8 HashMap + list by the array consisting of an array of HashMap is the main, the list is mainly to resolve hash collision exists ( "zipper method" to resolve the conflict). After JDK1.8 has made significant changes in resolving hash collision, when the chain length is greater than the threshold value (the default is 8), into a red-black tree list to reduce the search time
  • LinkedHashMap: LinkedHashMap inherited from HashMap, so it is still based on the underlying structure is a hash zipper and a list or an array composed of red-black tree. Further, a LinkedHashMap on the basis of the above structure, an increase of a doubly-linked list, so that the above structure can be maintained on the key insertion order. Simultaneously by corresponding operation of the linked list, the access order to achieve associated logic. Details can be viewed: "LinkedHashMap detailed source code analysis (JDK1.8)"
  • Hashtable: + list consisting of an array, the array is subject HashMap, the list is mainly to resolve hash collision exists
  • TreeMap: red-black tree (self-balancing binary tree sort)

How to choose the set?

On the selection of the set at 1. Map interface to obtain needed values ​​based on a key element

2. Select TreeMap need to sort

3. HashMap chose not require sorting

4. The need to ensure the security thread on the selection of ConcurrentHashMap

5. When we only need to store element values, choose a set of interfaces implemented Collection

6. The need to ensure that the only element when choosing realize Set set of interfaces such as HashSet or TreeSet

7. List does not need to choose to implement such interfaces ArrayList or LinkedList then be set according to the characteristics of the implementations of these interfaces.

Guess you like

Origin www.cnblogs.com/MWCloud/p/11234370.html