Collection in Java

A, set into the java Value (Collection), Key - Value (Map)

Stored values is divided into list and set.
list is ordered, repeatable
set are unordered, can not be repeated. The determination equals and hashcode, an object that is to be stored in the set, and must override equals hashcode method

B, list implementation classes: ArrayList, Vector, LinkedList (bidirectional circular linked list)

1. The underlying data structure

ArrayList, Vector underlyingRely on an array of high query efficiency, low efficiency deletions (Vector is thread-safe, the overall efficiency is lower than the ArrayList)
LinkedList bottomDependent way circular linked list, additions and deletions high efficiency, low query efficiency

2. The terms of storage elements

ArrayList, Vector, LinkedList elements in an ordered, repeatable allow null values,

3. capacity expansion

A 1.5-fold expansion ArrayList
Vector The incremental expansion increments 0, twice the capacity expansion; + delta otherwise original capacity
LinkedList no expansion

4. Thread safety

ArrayList, LinkedList thread safe(If there are multiple threads need to access the List of elements in the collection, consider using a collection of collections Collections packaged into thread-safe)
Vector thread-safe

C, set interface classes: HashSet, LinkedHashSet, TreeSet

HashSet

  1. You can not have the same elements,You may have a null elements, Deposited elements are unordered
  2. The underlying data structure is a hash table, the hash table is stored in a series of tables, the hash value is generated by the object hashCode ()
  3. The only way to ensure that two of: hashCode () and equals () method
  4. Non-thread-safe

LinkedHashSet

  1. You can not have the same elements,You can have a null value, The element loaded in strict accordance with order
  2. Underlying data structure composed by a hash table and chain
  3. I.e. an ordered list to ensure the extraction and storage elements consistent with the elements of the hash table to ensure uniqueness
  4. Non-thread-safe

TreeSet

  1. You can not have the same elements,Not have a null value, Sorted according to the natural ordering of the elements
  2. The underlying data structures is red-black tree (a self-balancing binary search tree)
  3. Non-thread-safe

D, Map's implementation class: HashMap, HashTable, TreeMap, LinkedHashMap

HashMap

  • Thread-safe, high efficiency. Null null values ​​and keys allowing

  • Map interface is based on a hash table. The role of the hash table is used to ensure uniqueness of the key.

  • Examples of common methods:

     new HashMap<Object,Object>();
    

HashTable

  • Thread-safe, low efficiency. Does not permit null keys and null values

Laidakedःashanap

  • It is based on the hash table to achieve an orderly and a list of links to the Map interface

  • By a hash table to ensure key uniqueness

  • (Same storage order and withdrawn) from the sorted linked list guaranteed bond

  • Examples of common methods:

    new LinkedHashMap<Object,Object>();
    

TreeMap

  • Map interface is to achieve red-black tree based

  • Construction method

    TreeMap();  //使用键的自然顺序构造一个新的、空的数映射
    //构造一个新的、空的树映射,该映射根据给定比较器进行排序
    TreeMap(Comparator<?super K>comparator)
    

The difference between HashMap and HashTable, HashTable with the ConcurrentHashMap

  1. The HashMap is null or a Key Value, which is not possible HashTable
  2. HashMap is not thread-safe, high efficiency. And HashTable are thread-safe, low efficiency
  3. ConcurrentHashMap also thread-safe, it is to a large Map, Map and is divided into several small plus lock respectively, and therefore more efficient than HashTable.

Guess you like

Origin www.cnblogs.com/siheng/p/12549694.html
Recommended