An ordered sequence of list--in Java

Source: Internet
Author: User

The list is an ordered sequence in Java:

One, the capacity

There is a capacity concept in ArrayList that represents the size of the underlying array (default is 10 without parameters). The capacity is automatically increased when needed (such as the add operation). LinkedList does not have this concept.

The TREEMAP also has a capacity, which defaults to 16.

Second, the improved search method

LinkedList and ArrayList are very inefficient. Such as Collection's contain and remove methods. They all spend linear time. The appropriate algorithm can be used.

Third, the improved Remove method (this method is invalid for ArrayList, ArrayList because remove changes the array structure, restructuring will take a lot of time. )

Limitations of foreach

public static void RemoveEvensVer2 (List<integer> lst) {for    (Integer x:lst) {        if (x% 2 = = 0)            Lst.remov E (x);}    } Throw Exception Java.util.ConcurrentModificationException

Use the while loop (but this method has several bad actions for LinkedList: 1, using the Get method, going through, and the get also spends linear time when traversing.) 2,remove Similarly, the Remove method itself also spends linear time)

public static void RemoveEvensVer1 (List<integer> lst) {        int i = 0;        while (I < lst.size ())            if (lst.get (i)% 2 = = 0)                lst.remove (i);            else                i++;    }

The iterator comes with the Remove handling LinkedList:

when iterating to its node, the elements of the Remove node are processed, and only the constant time is spent.  In addition, LinkedList, in addition to the iterator method provided by the Collection excuse, has an extension that listIterator() returns an extended interface Listiterator<e> Hasprevious () pevious () Add (Anytype x) set (Anytype newval) method. It is primarily convenient to iterate forward from the end, and to add and change values during iterations (for linkedlist changes and adding values are less expensive.) )

public static void RemoveEvensVer3 (List<integer> lst) {    iterator<integer> ITR = Lst.iterator ();    while (Itr.hasnext ())        if (Itr.next ()% 2 = = 0)            itr.remove ();}

Iv. sort

For ordered sequences, you can use Collections.sort (list<t> List, comparator< super t> C) to sort by your own requirements.

For Treeset/treemap based on a red-black tree data structure, you can sort by the natural order of the elements, or by creating a set Comparator .

Five, the difference between ArrayList and LinkedList, Array

An Array fixed length that can contain primitives base types and objects;

ArrayList is based on an array structure and is an implementation of a variable size array. ArrayList can only contain objects, and generics are available;

LinkedList is based on the two- way linked list structure, and lists interfaces are implemented as links. Implements all optional list operations, and allows all elements (including null). In addition to implementing the list interface, theLinkedList class provides a uniform naming method for the get,remove , and insert elements at the beginning and end of the list. These operations allow the link list to be used as a stack, queue, or double-ended queue (which implements the queue and Deque interfaces).

An ordered sequence of list--in Java

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.