Java Collection---list collection

Source: Internet
Author: User

Concept

The list inherits from the collection interface. A list is an ordered collection of elements in a list that can be acquired/deleted/inserted based on the index (ordinal number: The position information of the element in the collection).

Unlike set sets, the list allows repeating elements. For E1 and E2 object elements that meet the E1.equals (E2) condition, they can exist in the list collection at the same time. Of course, there is also a list implementation class that does not allow the existence of duplicate elements. At the same time, the list also provides a listiterator () method that returns a Listiterator interface object, and the addition, deletion, and setting of the Listiterator add element, as compared to the iterator interface, and can be traversed forward or backward, A concrete way to look down. The implementation classes of the list interface are mainly Arraylist,linkedlist,vector,stack and so on.

ArrayList class

ArrayList implements a variable-size array. It allows all elements, including null. ArrayList is not synchronized. Size,isempty,get,set method run time is constant. But the Add method cost is the allocated constant, and adding n elements requires an O (n) time. Other methods run at a linear time.
Each ArrayList instance has a capacity (capacity), which is the size of the array used to store the elements. This capacity automatically increases as new elements are added, but the growth algorithm is not defined. When you need to insert a large number of elements, you can call the Ensurecapacity method before inserting to increase the capacity of the ArrayList to improve insertion efficiency.

The difference between ArrayList and vector

ArrayList Vector
Performance High performance thanks to asynchronous processing Low performance thanks to synchronous processing
Thread Safety Non-thread safe Thread-safe, requires thread-safe use

Main methods

    • Public boolean Add (object?o): add element
    • public void Add (int index, Object Element): add element at specified location
    • Public Iterator Iterator (): Gets the Iterator object for easy traversal of all elements
    • Public Object Get (Int?index): Gets the element at the specified position according to the index
    • Public Object Set (int index,object element): Replaces the element at the specified position

Sorting methods

    • Collections.sort (List List): Sort the elements of a list naturally
    • Collections.sort (List list, Comparator Comparator): Custom ordering of elements in list

LinkedList class

The LinkedList implements a list interface that allows null elements. Additionally LinkedList provides an additional Get,remove,insert method at the first or the tail of the LinkedList. These operations make the LinkedList available as a stack (stack), queue, or two-way queue (deque).
Note LinkedList does not have a synchronization method. If multiple threads access a list at the same time, you must implement access synchronization yourself. One workaround is to construct a synchronized list when the list is created:
List List = Collections.synchronizedlist (new LinkedList (...));

Java Collection---list collection

Related Article

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.