Java Collection, java Collection class details
1. What is a set?
Objects are stored in containers. The object-oriented language represents objects. Therefore, to facilitate operations on multiple objects, you can store objects, A set is the most common way to store objects.
The collection is used to hold objects. Can store any type of objects, and the length is variable. The program may not be able to know how many objects are needed in advance. If an array is used to hold objects, the length is not well defined, and the Set solves this problem.
2. Differences between a set and an array
Arrays and collection classes are both containers.
The array length is fixed, and the set length is variable. The basic data types can be stored in arrays. A set can only store objects. The data types stored in arrays are single, and any types of objects can be stored in collections.
Features of collection classes
It is used to store objects with variable length. Different types of objects can be stored.
2.1. disadvantages of Arrays
A single data container with a single storage type and complicated operations (once declared as an array, it cannot be changed) CRUD
1.3. Classification of Sets
What does a set do?
1: add an object to a set
2: delete objects from the collection
3: Find an object from the collection
4: adding, deleting, modifying, and querying an object from the set
Note: The collections and arrays store object references instead of objects themselves.
Java engineers define different containers. Although containers are different, there are still some commonalities that can be extracted and finally a top-layer interface is extracted, so a collection framework is formed. How to Learn? Of course, it is learned from the top layer. The top layer has the most common and basic behavior. For specific use, You must select a specific container. Why? Because constantly extracting objects may not be able to create objects, the possibility of abstraction is very high, and there are more methods for subclass objects. Therefore, we need to look at the top layer to create the bottom layer. So what is the top layer of the Set called Collection?
Collection framework system
Collection: a single column set
List: The storage sequence is available and can be repeated.
ArrayList: array implementation, fast search, slow addition/Deletion
Because it is an array implementation, the array will be involved in addition and deletion.
Increasing capacity and copying elements. Therefore, it is slow. Arrays can be directly
Search by index, so the search speed is faster
Slow list: Linked List Implementation, fast addition/deletion, and slow Query
Due to the implementation of the linked list, when adding a table, you only need to let the previous element remember
You can do it yourself. When you delete the object, let the previous element remember the last element.
Element, and the last element remembers the previous element. Such addition and deletion Effects
High rate, but the Query Needs to traverse one by one, so the efficiency
Relatively low
Vector: the principle is the same as that of ArrayList, but the thread is secure and the efficiency is slightly lower.
The implementation method is the same as that of ArrayList, but thread security is considered.
Low Efficiency
Set: No storage order, not repeated
HashSet
TreeSet
LinkedHashSet
Map: Key-Value Pair
HashMap
TreeMap
HashTable
LinkedHashMap
Why are there so many collection containers? Because each container stores data differently, this storage method is called data structure)
Note that both the set and array store object references.
4. What kind of set should I use?