Thinking in Java - Study Notes - (11) Holding Objects

Java Programming Ideas - Chapter 11 - Holding Objects

If a program contains only objects with fixed glucose tolerance and whose lifetimes are books, then this is a very simple program.

Often, program problems create new objects based on some condition known only at runtime. Until then, the number of objects required, or even the exact type, will not be known.

Arrays are the most efficient way to store a set of objects, but arrays have a fixed size, and in the more general case you don't know how many objects you'll need when you write your program, or if you need a more complex way to store them object, so the fixed size of the array is too restrictive.

The Java utility class library also provides a fairly complete set of window classes to solve this problem, the basic types of which are List , Set , Queue and Map . (container class)

Generics and type-safe containers

Generics: Type parameters enclosed in angle brackets (there can be more than one), which indicate the types that this container instance can hold.

    ArrayList<Integer> array = new ArrayList<>();

By using generics, you can prevent objects of the wrong type from being placed into the container at compile time .

basic concept

The purpose of the Java Container Class Library is to "hold objects" and divide it into two distinct concepts:

1) Collection . A sequence of independent elements that all obey one or more rules. Lists must hold elements in the order they were inserted, while Sets cannot have duplicate elements. Queue determines the order in which objects are created according to exclusion rules .

2) Map . A set of "key-value" objects that allow you to use the key to look up the value. ArrayList allows you to look up values ​​using numbers, so in a sense it associates numbers with objects. A mapping table allows us to look up an object using another object , it is also called an " associative array " because it associates some objects with other objects, or a "dictionary" because you can Use the key object to look up the value object.

iterator

Iterator : The ability to separate the operations of traversing the sequence from the underlying structure of the sequence.
Because of this, we sometimes say that iterators unify access to containers.

ListIterator

ListIterator is a more powerful subtype of Iterator that can only be accessed by various List classes. While an Iterator can only move forward, a ListIterator can move in both directions. It can also yield the indices of the previous and next elements relative to the current position pointed to by the iterator in the list, and can use the set() method to replace the last element it has visited.

LinkedList

LinkedList adds methods that can be used as a stack, queue, or deque.

Stack

Last In First Out (LIFO).

Queue

First in first out (FIFO).
Priority queue: PriorityQueue

Summarize

Based on jdk1.5

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324686533&siteId=291194637