Those things in the Java container-collection

Source: Internet
Author: User

1. First, let's talk about the collection interface first.

Collection interface:

Collection is the most basic set interface, which is a sequence of independent elements that serve one or more rules. A collection represents a set of object, the collection element (Elements).

Some collection allow the same elements, others do not, some can be sorted, others do not, and the Java SDK does not provide an interface that inherits collection directly, but rather inherits some of collection's sub-interfaces, such as the List,set interface.

Some methods of collection:

2. Then say the list interface, the set interface and the map interface, of course, by the way the queue queue

List interface:

The list is an ordered collection that preserves elements in the order in which they are inserted, enabling the interface to precisely control where each element is inserted, allowing the user to access the elements in the list based on the index.

Common classes that implement the list interface are: Linkedlist,arraylist,vector and stack.

Set interface:

At the core point, set is the collection (example: E1 and E2 have E1.equals (E2) =false) that do not contain duplicate elements , and allow null elements, but only one null element at most.

The set container class mainly has hashset and treeset and so on.

      

Map interface:

The map and List,set interfaces are different and do not inherit collection. It is a pair of "key-value pairs" objects that allow keys to look up values.

Map provides a key-to-value mapping, a map cannot contain the same key, and a key can only map a value (one-to-one relationship).

The common classes that implement the map interface are: Hashtable,hashmap,weekhashmap.

       

Main method: 1.boolean equals (object o) Compare object 2.boolean Remove (object o) to delete an object 3.put (object Key,object value) to add key and Value4. Hashtable class

 

Queue interface:

Queue (also known as "queues") it is a special linear table that determines the order in which objects are produced, in accordance with the queuing rules.

In simple terms, according to the "FIFO" rule, the front-end portion of the table is used as the delete operation, the back-end part is used as the insert operation, the front-end deletion part is called "the enemy"; the back-end insert part is called: "Team Tail". If the queue is empty, it is also called: "Empty queue".

It is worth mentioning that LinkedList also implements the queue interface, so you can also use LinkedList as a queue.

3. Explore the common classes that implement the list interface: Linkedlist,arraylist,vector and Stack

LinkedList class:

LinkedList implements the list interface, allows null elements, and is thread insecure (non-thread safe), and cannot be accessed randomly, because the underlying is implemented through a linked list, to remove or insert an object from a linked list, you only need to change the reference of the object before and after it. So in terms of speed, the relative ArrayList is faster.

In addition, LinkedList provides some additional methods: Get (), remove (), insert () as the header and tail of the LinkedList, which can be used primarily as a stack (stack), queue, 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:list list = collections.synchronizedlist (new LinkedList (...) when the list is created. ;

ArrayList class:

ArrayList implements a variable-size array that allows all elements, including null elements, and is also thread insecure (non-thread safe), but allows random access because it is a sequence structure (array), which itself is suitable for random access.

[] Array, the structure of Vector,arraylist is the performance of O (1) In terms of random access, traversal and acquisition size.

Note: Like LinkedList, ArrayList is also non-synchronous (unsynchronized). In general, use both of these. Because of the non-synchronous, so the efficiency is higher, if it involves the stack, queue and other operations, should consider using the list, for the need to quickly insert, delete elements, should use LinkedList, if you need to quickly randomly access elements, you should use ArrayList.



Vector class:

and ArrayList very similar, the difference is that it is thread-safe , iterator (iterator) created by the vector, itself and ArrayList created by the iterator (iterator) is the same interface, But because vectors are thread-safe, when a iterator (iterator) is created and used, another thread changes the state of the vector (for example, "add" or "delete" some elements), and the method that calls the iterator (iterator) throws an exception: " Concurrentmodificationexception", so this exception must be captured.

Stack class:

Inheriting vectors, implementing the "last in first out " stack, Stack provides 5 ways for vectors to be used as stacks, the basic push () and Pop () methods, and the Peek method to get the stack top element, the empty method to determine whether the stack is empty, The search method detects the position of an element in the stack.

Note: Stack has just been created as "empty stack "

Those things in the Java container-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.