[introduction to Java] day18 Java container class (I) collection interface

Today, let's take a look at a big guy in Java, that is, container.

The so-called container is specifically used to hold objects. If you have learned advanced numbers, yes, it is the same concept as the collection mentioned in it, which is the collection of a pile of objects. However, the collection class is a subset of the container class. For different representation, it is still called the container class. The later collection class is only a subset of the container, which will be introduced in detail later.

Container is used to store and manage other class objects. You can understand it as warehouse keeper. When you have something to store and manage, you should remember to find it. You might say, isn't there an array? Indeed, it is also a good choice to store a pile of objects of the same type with an array, but there is a big defect, that is, the size of the array can only be fixed, and an object cannot be dynamically added or deleted from the array. To expand the capacity, you can only create an array, and then copy all the original objects into the new array, And it can only store objects of the same type, which is not flexible enough to use. But our housekeeper is different.

International practice, let's look at a chestnut first:

The output is as follows:

I believe that after reading the story of Xiao Ming and the housekeeper, you should have a preliminary understanding of the concept of container. Generally speaking, a container is a good housekeeper who can manage objects when you need to store a series of objects.

Of course, HashMap is not the only housekeeper in the container family. It was said at the beginning that containers are a huge family. Let's take a look at a picture and feel it:

Seems a little too much? The relationship is a little complicated. Yes, except for the collection classes in the merge contract, most container classes are almost here. This figure, emmmm Just have a look. Let's look at the picture below

Don't panic. In fact, these are the most commonly used. Collection and map are two large interfaces. There are three sub interfaces under collection, list, queue and set. The following are the three most commonly used classes, ArrayList, LinkedList and HashSet. The most commonly used under the map interface is the HashMap in the chestnuts above. As you can see, there are many different implementation classes in the container class, that is, different housekeepers. They have different abilities and have their own strengths and weaknesses. As for their specific introduction, they will be introduced in the next few articles. This article, as an introduction to the collection, will not explain more.

It should be noted that the container can only store objects, not basic types. So when you put an int data 1 into the container, it will be automatically boxed, converted into an integer class and stored. Each basic type in Java has a corresponding reference type. References to multiple objects are stored in the container, and the object itself is still placed in heap memory. Containers can store different types and unlimited number of data types.

Collection interface

The collection interface is the big brother in the container family and the most basic container interface, but the collection here is not equivalent to the container, because you can see from the above figure that there is another big brother in the container family, that is, the map interface. A collection represents a group of objects, that is, the elements of a collection. There are three sub interfaces under the collection interface, namely list, set and queue. They have their own characteristics, which will be introduced one by one below. However, they all inherit from the collection interface, so they inherit all the characteristics of the collection

Let's take a look at the methods of the collection interface:

It can be seen that there are still quite a lot of collection operations, including addition, deletion, query and batch operations. As for what iterators are, they will be described in detail in the following chapters. The last two methods involve flow operation, which is a new feature added in Java 8. I won't talk about the knowledge of flow operation for the time being. I'll explain it later.

Through this article, you only need to know what a collection is, why there is a collection, the overall picture of the collection family, and what methods are available in the collection interface. Later articles will introduce the container family from the following aspects:

  1. Map interface

  2. Iterable interface

  3. List, set, queue interface

  4. ArrayList usage and application scenarios + source code analysis

  5. HashSet usage and application scenarios + source code analysis

  6. LinkedList usage and application scenarios + source code analysis

  7. HashMap usage and application scenarios + source code analysis

That's all for today's explanation. I just introduced the basic concept of container. As an appetizer for container learning, the following series of articles will focus on container. I hope you will continue to pay attention!

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
分享
二维码
< <上一篇
下一篇>>