Java Container Learning: List, Set, Queue, Map

Source: Internet
Author: User

First, the container

Many times, the program always creates new objects based on certain conditions that the runtime knows about, and before that the number of objects required, and even the type of the object, is required to hold the object. The Java container class can automatically adjust its size. A container class library can be divided into two different concepts:

    1. Collection. A sequence of independent elements, such as list (which saves elements in order of insertion), Set (cannot have duplicate elements), and queue (determines the order in which objects are produced by queuing rules);
    2. Map. A pair of "key-value pairs" objects that allow keys to look up values. Map maintains a mapping table, also known as "associative array" or "dictionary."

There are two types of lists that save elements in the order in which they are inserted:
    1. ArrayList, random access elements are faster, but inserting and removing elements in the middle of a list is relatively slow;
    2. LinkedList, the random access element is slower, but inserting and removing elements in the middle of the list is faster, providing an optimized sequential access.

Stack, "stack", is a "LIFO" container, the last element pressed into the stack, the first pop-up stack. LinkedList has the ability to directly implement all the functions of the stack, you can use the LinkedList as a stack.


Second, iterators

An iterator is an object that works by traversing and selecting an object in a sequence, and the client programmer does not have to know the underlying structure of the sequence, that is, whether the sequence structure is ArrayList, LinkedList, or hashset, you can iterate through the sequence with iterators. The Iterable interface contains a iterator () method that can produce iterator, and the Iterable interface is used by foreach to move through the sequence, so if any iterable class is created, it can be used in a foreach statement. The collection class (but not the map) is a iterable type, and map has to use the EntrySet () method to produce a set consisting of the elements of the map.entry, because set is a iterable, so you can use a foreach loop.


Finally look at the Java container diagram, the black box is a common container.




Java Container Learning: List, Set, Queue, Map

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.