Summary of Chapter 11 of the fourth edition of Java programming thought

1. Container classes are divided into two categories: collection and map

2. When defining collections, it is a good habit to use upward transformation

  List
list = new ArrayList

();

Note: ArrayList has been transformed upward to list. The advantage of this is that if you want to use other list implementation classes, it is also possible. The disadvantage is that there are some additional methods in ArrayList that are not included in the list. If you need to call these methods, you also need to use ArrayList to define them.

3. Use of collection

4. Stack

Stack: first in, last out (LIFO), sometimes stack is also called stack stack, because the last "pushed in" pops up first.

LinkedList has methods that can directly implement all the functions of the stack. Therefore, LinkedList can be used directly as a stack.

In other words, there are methods in the LinkedList that are first in and last out.

Operation results:

Through this case, we can see that the so-called first in and last out means that add comes in last and remove goes out first It has nothing to do with sorting

5. Queue

A queue is a typical first - in - first - out container That is, put it from one section of the container and take it out from the other end And the order in which things are put into the container is the same as the order in which they are taken out.

LinkedList provides methods to support the behavior of queues. And it implements the queue interface. Therefore, LinkedList can be used as an implementation of queue. By transforming LinkedList upward into queue, the usage of queue is shown below.

Here, you want to put the offer used by the element in the queue, take out the peek used by the element, and delete the remove used by the element. Put it in first and take it out first.

When we talk about stack, we see that LinkedList can realize stack in first out. When you see the queue of the queue, you say that LinkedList can implement queue first in first out. What's going on? Let's take a look at the API. That's what happened

6. PriorityQueue: priority queue

The priority queue declares that the next pop-up element is the most needed element. That is, it is the element with the highest priority. When you use the offer method to access an object, the object will be sorted in the queue. The default order uses the natural order of objects in the queue. But you can also modify this order through your own comparator.

PriorityQueue ensures that when you call the peek(), poll(), remove() method, the obtained element will be the highest priority element in the queue

Operation results:

  1. The smaller the number, the higher the priority

  2. Spaces take precedence over letters

  3. Strings and characters can be converted into corresponding numbers for processing

7. Iterator

In Java, the iterator is used instead of the collection to represent the commonness between collections. However, implementing collection means that the iterator () method needs to be provided.

(unfinished, to be improved)

8. Foreach and iterators

Iteratable interface: this interface contains an iterator () method that can generate an iterator, and the iteratable interface is used by foreach to move in the sequence. Therefore, if the class you create implements the iteratable interface, you can use it in the foreach statement:

Operation results

9. Adapter method

We know that if a class implements the iteratable interface, it will override the iterator method that returns the iterator type. When we use it, we can use the foreach method to traverse the class. However, this way of implementing the interface can only have one traversal method. Suppose: I want to have a variety of traversal schemes now. For example: positive order traversal, reverse order traversal, how to achieve it? We use the adapter method. The code is as follows:

This example shows how I can define multiple foreach loops in a class. Next, we use this method to define two other loop methods for iterableclass:

This is about collection Shuffle() method Just look at the example below

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>