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:
- 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);
- 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:
- ArrayList, random access elements are faster, but inserting and removing elements in the middle of a list is relatively slow;
- 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