Old readers all know that I switched to java by self-study. At that time, under the pressure of survival, I learned more quickly, and many knowledge points only stayed at the level that I could use. Recently, it is meaningless to use light without knowing the principle. Each use is mechanical and “skilled”. In addition, there has always been the idea of returning to basics, so I want to resume my basic knowledge of java in my spare time. Knowing what it is and why it is is the pursuit of technicians.
When it comes to the basis for resuming trading, not all players will resume trading, and it is not necessary to do so without that time. For example, some basic syntax and conditional statements are extremely simple. This kind does not need to be restocked. After much deliberation, I think the whole plan should start from the collection, and the way to resume is to read the source code.
1、Why does Java have collections?
First of all, java is an object-oriented language, operating objects is our daily. Since the operation requires something to store the object. So containers came into being. The first container that beginners come into contact with is array, but this is far from enough. According to different objects and different businesses, we need to use different containers. For example, if we don’t want to repeat objects, we will think of using set containers. If we want objects to be orderly, we will use List. Either List or Set. They all have commonalities, and java provides us with Collection collections based on these commonalities.
2、Collection interface frame diagram
From the above Collection interface framework diagram, we can know that Collection is the parent interface of List, Set and Queue. seeing this, you may ask, where is Map? In fact, the containers in Java, including Collection and Map, Map is another system, so we won’t talk about Map today.
The Collection interface defines the above methods to be implemented. For example:
- Size () Calculates Container Length
- Is isEmpty () empty
- Does CONTAINS () contain an object
- Does containsall () contain all objects of another collection
- Iterator () The method of the upper interface iterable, which is used to generate iterative objects and traverse the objects
- Add () adds an object
- Add () adds all objects of another collection
- Remove () removes an object
- Remove all () removes all objects
- ToArray () converts a collection to an array
- Does RETAIN ALL () intersect with another set
List collection is characterized by orderly and repeatable elements, which includes three implementation classes: ArrayList, Vendor and LinkedList. The comparison of the three is as follows:
The biggest feature of Set set is that repeating elements are not allowed. It has four implementation classes: HashSet, TreeSet, LinkedHashSet, EnumSet. The comparison is as follows:
As for EnumSet class, I haven’t studied how to use it. After checking on the Internet, it is a collection class specially designed for enumeration class. All elements in EnumSet must be enumeration values that specify enumeration types. Specific details will not be introduced here. After I study usage and scenarios, I will introduce them specifically.
Queue is seldom used. It is often used to simulate queue and realize FIFO and other data structures. Generally, queues do not allow random access to elements in the queue. I haven’t used it yet in my work. I’ll share it with you after I have studied it.