Summary of the Java collection

Source: Internet
Author: User

Reference blog:
http://www.jianshu.com/p/63e76826e852
Http://www.cnblogs.com/LittleHann/p/3690187.html
Https://github.com/pzxwhc/MineKnowContainer/issues/18
Parametric books
The 11th chapter of Java Programming thought

A diagram that illustrates the relationship of the Java Collection class to the Common collection class

As you can see from the frameset above, the Java Collection Framework consists of two types of containers, one set (Collection), one collection of elements, the other is a graph (map), and a key/value pair mapping is stored. Collection interface also has 3 seed types, List, set and queue, and then the following are some abstract classes, and finally the implementation of specific classes, commonly used are ArrayList, LinkedList, HashSet, Linkedhashset, HashMap, Linkedhashmap and so on.
A: List collection
The List interface is a subclass of the collection interface, and the method is expanded on the basis of collection. At the same time, each element can be inserted in the precise control of the position, its main implementation class has arraylist,vector,linkedlist.

1.1 ArrayList
ArrayList implements the List interface, which means that you can insert null values, or you can insert duplicate values, non-synchronous, which is an implementation based on an array. From default_capacity = 10 in the source code, the default allocation below is an array of length 10.

ArrayList is not suitable for adding or removing operations, you can first see this sentence: it elementData = Arrays.copyOf(elementData, newCapacity); is necessary to know that the internal implementation of the ARRAYS.COPYOF function is to create an array, and then copy the old array values into the new array. When the operation is often increased, when the capacity is not enough, it will carry out the above-mentioned expansion operation, so that the performance is naturally down. Or, when we make additions and deletions in a fixed position, it can be System.arraycopy(elementData, index, elementData, index + 1, size - index); very inefficient.

Analysis of the reasons for the inefficiency, then we can know: if we need to constantly do a specific location of additions and deletions, then it is best not to use this, but, if we basically do not have fixed location of the addition and deletion operations, it is best to estimate the size of the data volume, and then initialize the minimum capacity, This can effectively avoid the expansion. The following code:

   ArrayList<Integer> arrayList = new ArrayList<Integer>(20);

ArrayList Summary:

      1. ArrayList can insert a null value, or you can insert a duplicate value
      2. ArrayList is an array-based time, so many of the array's properties are applied directly to the ArrayList.
      3. The performance consumption of ArrayList is mainly derived from the expansion and fixed location of additions and deletions.
      4. ArrayList when creating, you need to consider whether you want to initialize the minimum capacity to avoid the consumption of expansion.
      5. ArrayList not suitable for insert and delete operations
      6. ArrayList is not thread-safe
1.2 Vector

Also implements the List interface, so you can insert a null value, and you can insert a duplicate value. Like HashTable, it belongs to a synchronous container, not a concurrent container. (Refer to "Java Concurrency Programming Combat", similar to copyonwritearraylist,concurrenthashmap this belongs to the concurrent container)

Internal member variables:

protected Object[] elementData;public Vector() {    this(10);}

As you can see, it is also an array-based implementation, and initialization is also 10 capacity. So, let's see if the Add () method is the same as ArrayList.

public synchronized boolean add(E e) {    modCount++;    ensureCapacityHelper(elementCount + 1);    elementData[elementCount++] = e;    return true;}

Can see, and ArrayList is the same, just add synchronized to synchronize, in fact, many other methods are added synchronized to achieve synchronization.

Vector Summary:
    1. You can insert a null value, or you can insert a duplicate value
    2. is also based on the time of the array, so many of the features of the array are applied directly to the Vector.
    3. Performance consumption is also mainly derived from the expansion.
    4. When you create it, you need to consider whether you want to initialize the minimum capacity to avoid the consumption of the expansion.
    5. Equivalent to the thread-safe version of ArrayList, the way to achieve synchronization is through synchronized.
    6. Vector is not suitable for insert and delete operations
1.3 LinkedList
  The LinkedList implements the List interface, so the LinkedList can also be placed in duplicate values, or they can be placed into null values. LinkedList does not support synchronization. Unlike ArrayList and vectors, LinkedList is a data structure that uses a linked list and is no longer an array.
When the additions and deletions, only need to change the pointer, and will not be like an array of large-scale movement of the overall data, copying and other performance-consuming operations.

But when it comes to traversal, ArrayList is better than LinkedList.


Two: Set
The set interface is extended from collection, which differs from the list in that it specifies that the instance of set does not contain duplicate elements. Within a ruleset, there must be no two equal elements. Abstractset is an abstract class that implements the set interface, and the set interface has three concrete implementation classes, namely the hash set HashSet, the chain hash set Linkedhashset, and the tree set TreeSet.
Set determines that two objects are the same not using the "= =" operator, but rather by the Equals method
   2.1 HashSet   HashSet is a typical implementation of the set interface, HashSet uses the hash algorithm to store elements in the collection, thus having good access and lookup performance. When an element is deposited into the HashSet collection, HashSet calls the object's  
2.2 linkedhashset The Linkedhashset collection also determines where the element is stored based on the hashcode value of the element, but unlike HashSet, it maintains the order of the elements using the linked list, which makes the elements appear to be saved in the order in which they were inserted.
2.3 SortedSet This interface is primarily used for sorting operations where subclasses that implement this interface are sub-classes of sort 2.4 TreeSet
TreeSet extended from Abstractset, and implemented the Navigableset,abstractset extended from Abstractcollection, the tree set is an ordered set, the bottom is a tree,   This makes it possible to extract an ordered sequence from the set. When instantiating TreeSet, we can assign a comparer comparator to TreeSet to specify the order of elements in the tree set. A tree-shaped set provides a number of convenient methods.
2.5 Enumset Enumset is a collection class designed specifically for enumeration classes, and all elements in the Enumset must be enumerated values of the specified enumeration type that are explicitly or implicitly specified when the Enumset is created. The set elements of the Enumset are also ordered,
They determine the order of the collection elements in the order in which the enumeration values are defined within the enum class

Three: Queue
The ueue is used to simulate a "queue" of this data structure (FIFO). The head of the queue holds the longest-held element in the queue, and the tail of the queue holds the element with the shortest time in the queue. new element insertion (offer) to the tail of the queue,
The Access Element (poll) operation returns the element in the queue header, which does not allow random access to elements in the queue. It's a good idea to combine the usual queues in your life. 3.1PriorityQueue Priorityqueue is not a more standard queue implementation, Priorityqueue the order in which the queue elements are saved is not in the order in which the queues are queued, but in terms of the size of the queue elements, which is also available from its class name
Look out. 3.2 Deque Deque interface represents a "double-ended queue", the double-ended queue can be added from both ends, delete elements, so the Deque implementation class can be used as a queue, can also be used as a stack 3.2.1) Arraydeque is an array-based, double-ended queue that, like ArrayList, uses a dynamic, redistributed object[] array to store collection elements that, when the collection element exceeds the capacity of the array, is heavily
Assign a new object[] array to store the collection element 3.2.2) LinkedList

Four: Map

Implementation classes: HashMap, Hashtable, Linkedhashmap, and TreeMap

HashMap

HashMap is the most commonly used map, it stores data according to the hashcode value of the key, can get its value directly according to the key, has the fast access speed, when traversing, obtains the data the order is completely random. Because key objects cannot be duplicated, HashMap allows a maximum of one record's key to be null, allowing the value of multiple records to be null, which is non-synchronous.

Hashtable

Hashtable, like HashMap, is a thread-safe version of HashMap, which supports thread synchronization, which means that only one thread can write hashtable at any one time, so it also causes Hashtale to be slow to write, and he inherits from the dictionary class, The difference is that it does not allow logging of keys or values to be null, and is less efficient.

Concurrenthashmap

Thread-safe, and lock-separate. Concurrenthashmap Internal Use Segments (Segment) to represent these different parts, each of which is actually a small hashtable, they have their own locks. As long as multiple modification operations occur on different segments, they can be performed concurrently.

Linkedhashmap

Linkedhashmap Save the insertion Order of records, when traversing Linkedhashmap with Iteraor, the first record must be inserted first, the traversal will be slower than HashMap, there are all the characteristics of hashmap.

TreeMap

TreeMap implements the Sortmap interface, the ability to sort its saved records according to the key, the default is the key value of the ascending sort (natural order), you can also specify a sort of comparator, when using iterator traversal treemap, the resulting records are sorted out. The key value is not allowed to be empty, non-synchronous.



Summary of the Java collection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.