Summary of Java Collections (i): Lists and queues

Source: Internet
Author: User
Tags array length

The specific container classes in Java are not built from scratch, and they all inherit some abstract container classes. These abstract container classes provide a partial implementation of the container interface, which facilitates the concrete implementation of the container class on the basis of the abstract class. The relational schema diagram for the container class and interface is as follows:

The dotted box represents the interface, with collection, List, Set, Queue, deque, and map.

There are six classes of abstract containers:

    • Abstractcollection: Implements the collection interface, is abstract class abstractlist, Abstractset, Abstractqueue inheritance, Arraydeque also inherits from the Abstractcollection (not pictured).
    • Abstractlist: The parent class is abstractcollection, implements the list interface, is ArrayList, Abstractsequentiallist inherits.
    • Abstractsequentiallist: The parent class is abstractlist and is inherited by LinkedList.
    • Abstractmap: Implements the Map interface, is TreeMap, HashMap, Enummap inherit.
    • Abstractset: The parent class is abstractcollection, implements the set interface, is inherited by HashSet, TreeSet, and Enumset.
    • Abstractqueue: The parent class is abstractcollection, implements the queue interface and is inherited by Priorityqueue.

These abstract classes provide the underlying implementation of the corresponding interface, while defining the abstract methods that some subclasses must override, equivalent to defining a template, which is somewhat similar to a design pattern-the template method pattern .

The following is a summary of the specific implementation of some of the details and characteristics of the class:

ArrayList

The internal use of the object array implementation, the default initial size is 10, can be specified manually, the expansion strategy is newcapacity = oldcapacity + (oldcapacity >> 1),
That is, each increase to 1.5 times times the original, but not less than the minimum value, the maximum capacity is integer.max_value, that is, 0x7fffffff. When expanding, a new array is created to copy the contents of the original array past.

No generic array e[] cause personal guess is because generics in Java are only supported in jdk1.5, and before that, object, in order to maintain forward compatibility has to do so, jdk1.6 in the new arraydeque inside is the generic array E []。

LinkedList
The internal use of the linked list implementation, can be used as a queue (queue), both ends can be manipulated, tail added, head view and delete. can also be used as a double-ended queue (Deque)(bidirectional linked list) (bidirectional traversal, the ability to implement the stack).

The internal only maintains the head and the tail node, the construction method cannot specify the size, allocates space.

Performance

    • Can not be accessed randomly, must start from the beginning or the end of the search, the efficiency of O (N/2) (using two-point method)
    • By content lookup efficiency is O (N/2)
    • Both ends add delete efficiency to O (1)
    • Intermediate Insert Delete First position, efficiency is O (n), but insert or delete itself is very efficient, O (1)

Arraydeque
This is a new class for jdk1.6. Internally maintains a generic array e[], using the head and tail two int indexes to represent the chain header and tail (where tail refers to the next empty space), making the physically simple array from beginning to end into a logically looped array that can be used as a list. The default initial array size is 16, and the scaling strategy is multiplied by 2

    • If head and tail are the same, the array is empty and the length is 0.
    • If tail is greater than head, the first element is Elements[head], the last is elements[tail-1], the length is tail-head, and the element index is from head to TAIL-1.
    • If tail is less than head and is 0, the first element is Elements[head], the last is elements[elements.length-1], and the element index is from head to Elements.length-1.
    • If the tail is smaller than head and is greater than 0, a loop is formed, the first element is Elements[head], the last is elements[tail-1], the element index is from head to Elements.length-1, and then from 0 to Tail-1.

The initial array size numelements can be specified by constructing the method, but the actual size is:

    • If numelements is less than min_initial_capacity, the allocated array length is min_initial_capacity, which is a static constant with a value of 8.
    • In cases where numelements is greater than or equal to 8, the actual length of the allocation is the smallest number that is strictly greater than numelements and is an integer power of 2. For example, if Numelements is 10, it is actually assigned 16, or 64 if numelements is 32.

The power of 2 is used to ensure

 Public void AddLast (e e) {        ifnull)            thrownew  NullPointerException ();         = E;         if ((tail = (tail + 1) & (elements.length-1)) = = head)            doublecapacity ()    ;

( tail + 1) & (elements.length-1) can be positioned to the correct next position tail, for example, if Elements.length is 8, then (ELEMENTS.LENGTH-1) is 7, The binary is 0111, for negative-1, and 7, and the result is 7, for a positive number 8, and 7, and the result is 0, can be reached in the loop array to find the next correct position.

After expansion, the array is rearranged, and head is 0,tail is the current number of elements.

Performance

    • Adding and removing elements at both ends is highly efficient, the memory allocations required for dynamic expansion, and the array copy overhead can be split, specifically, the efficiency of adding n elements is O (n).
    • The efficiency of finding and deleting elements based on content is low, O (N).

Simple summary:

Unlike ArrayList and LinkedList, Arraydeque does not have the concept of an indexed position and cannot operate according to the index location.
Arraydeque and LinkedList both implement the Deque interface, which one should be used? If you only need to deque interface, operate from both ends, generally speaking, arraydeque more efficient, should be used preferentially, however, if you need to operate according to the index location, or often need to insert and delete in the middle, you should choose LinkedList.

Summary of Java Collections (i): Lists and queues

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.