Features and usage of MAP, Set, List, Queue, stack

Source: Internet
Author: User

    • Map

The object that the key maps to the value. A map cannot contain duplicate keys, and each key can be mapped to at most one value.

Some mapping implementations can explicitly guarantee their order, such as the TreeMap class, while others do not guarantee the order, such as the HashMap class.

Map elements, you can extract the key sequence, the value sequence alone.
Use Keyset () to extract the key sequence and generate a set for all keys in the map.
Use values () to extract the value sequence and generate a collection for all values in the map.
Why a build set, a Build collection? That's because key is always unique, and value allows repetition.

    • Set

A collection that does not contain duplicate elements. More specifically, the set does not contain elements that satisfy e1.equals (E2) to E1 and E2, and contains a maximum of one null element.

Do not randomly access the contained elements

Only one-way traversal can be implemented with iterator

Set has no synchronization method

    • List

Random access to contained elements
Elements are ordered.
You can add and delete elements at any location
Element position unchanged no matter how many times it is accessed
Allow repeating elements
Implement one-way traversal with iterator, and also use Listiterator to implement bidirectional traversal

    • Queue

Advanced First Out

To avoid collection's add () and remove () methods as much as possible, use the offer () to add elements and use poll () to get and move out of the element. Their advantage is that the return value can be used to determine success or failure, and the Add () and remove () methods throw exceptions when they fail. If you want to use the front end without removing the element, use the element () or peek () method.
It is worth noting that the LinkedList class implements the queue interface, so we can use LinkedList as a queue.

Queue implementations generally do not allow the insertion of NULL elements, although some implementations, such as LinkedList, do not prohibit the insertion of NULL. Even in implementations that allow Nulls, NULL should not be inserted into the queue because Null is also used as a special return value for the poll method, indicating that the queue does not contain elements.

    • Stack

Last in, first out

The stack inherits from the vector (an array of objects that can grow) and is also synchronized
It extends the class vector by five operations, allowing the vector to be treated as a stack. It provides the usual push and pop operations, and the Peek method that takes the stack vertex, the empty method that tests if the stack is null, the search method that finds the item in the stack, and determines the distance to the top of the stack .

    • Usage

If you are involved in stacks, queues and other operations, you should consider using list;

For the need to quickly insert, delete elements, should use LinkedList;

If you need to quickly randomly access elements, you should use ArrayList.

If the program is in a single-threaded environment, or access is done only in one thread, it is more efficient to consider non-synchronous classes

Features and usage of MAP, Set, List, Queue, stack

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.