List in Java

Source: Internet
Author: User

belongs to the Java.util class package.

Collection is the most basic collection interface. The two interfaces that are derived from the collection interface are List and SeT.

First, List

The list is an ordered collection that uses this interface to precisely control where each element is inserted, and the user can use an index to access the elements in the list, similar to an array. The list allows the same elements, while set does not allow. A null element is allowed. In addition to the iterator method, which has the collection interface prerequisites, the list also provides a Listiterator method that returns a Listliterator interface, compared to the standard iterator interface. Listiterator a number of add-like methods that allow adding, deleting, and setting elements. can also traverse forward or backward. Inserts or removes elements into the middle.

Common classes that implement the list interface are ArrayList, LinkedList, vectors, and stacks. Where the stack class inherits from the Vector class.


ArrayList: (1) is implemented by an array . (2) The advantage is the random access element, but the insertion and removal of elements in the middle of the list is slow. Instead of inserting or deleting elements (because that's a lot more expensive than linkedlist), Listiterator should only be used to traverse ArrayList right and backward. (3) A variable-sized array is implemented. By default, the original 50% is automatically increased and the initial default capacity is 10. (4) is asynchronous, and the object in ArrayList is not thread-safe.


LinkedList: (1) implemented by a doubly linked list . (2) sequential access is optimized, and the cost of inserting or deleting elements into LinkedList is not significant. Random access is relatively slow. (3) The following methods are also available: AddFirst (), AddLast (), GetFirst (), GetLast (), Removefirst (), Removelast (), which enable LinkedList to act as a stack, Queues and two-way queues are used . (4) The LinkedList is asynchronous and there is no synchronization method. If multiple threads access the list at the same time, you must implement access synchronization yourself. One approach is to construct a synchronized list when creating a list, by: List List=collection.synchronizedlist (new LinkedList (...));.


Vector:(1) A variable size array is implemented by the array implementation (2). By default, it automatically grows by one times, with an initial default capacity of 10. (3) is synchronous , the object in the vector is thread-safe, but the requirement of synchronization will affect the efficiency of execution. (Very similar to ArrayList, but synchronous)

Stack: inherits from the vector class and implements a last-in-first-out stack. An additional 5 methods are implemented: push-to-stack, pop-out-stack, peek-get stack-top element, empty checks if the stack is null, and search detects the position of the element in the stack.


Second,Set

Set: (1) is a collection that does not contain duplicate elements. (2) There is a maximum of one null element. (3) The interface is identical to the collection. (4) The order of maintenance elements is not guaranteed.

Set sub-class: HashSet, TreeSet, Linkedhashset.

HashSet : Set for quick find design. the object that is deposited into the hashset must define HASHCODE (). Unordered, can put a null.

TreeSet : The set of the hold order, the bottom is the tree structure. Use it to extract ordered sequences from the set (automatically sequenced). Null values are not allowed.

Linkedhashset : has hashset query speed, internally maintains the order of elements in the list (inserted order). When you use iterators to traverse a set, the results are displayed in the order in which the elements are inserted.

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.