Collection collection summary, Collection set
I. Overview of Collections
1. Origins of Collections
Java is an object-oriented language, while object-oriented language describes things through objects. To facilitate operations on multiple objects, we must store these multiple objects. To store multiple objects, we should not be a basic variable, but a variable of the container type. In the basic knowledge of Java, what types of containers do we have? The answer is array and StringBuffer, but what? The StringBuffer result is a string that does not necessarily meet our requirements. Therefore, we can only select an array, which is an array of objects. The object array cannot adapt to the changing needs, because the length of the array is fixed. At this time, Java provides a collection class for us to adapt to the changing needs.
2. Differences between a set and an array
Since we have mentioned arrays and collections, let's talk about the differences between collections and arrays:
A: length difference
Fixed Array Length
Variable set length
B: The content is different.
Arrays store the same type of elements.
Different types of elements can be stored in a set.
C: The data types of storage elements are different.
Arrays can store basic data types or reference data types.
The set can only store Reference Data Types
Ii. Framework of Collection
1. Collection System
The Collection set is the top-level interface of a single column set. Collection represents a group of objects, also known as Collection elements. The data structure of the Set (simple note is the data storage method) is effective for the set element.
Functions of Collectoin:
1) add features
Boolean add (Object obj): add function
Boolean addAll (Collection c): Add a Collection Element
2) Deletion
Void clear (): removes all elements.
Boolean remove (Object obj): removes an element.
Boolean removeAll (Collection c): removes the elements of a Collection (returns true if one element is removed)
3) Judgment Function
Boolean contains (Object obj): determines whether the set contains the specified element.
Boolean containsAll (Collection c): determines whether the set contains the specified set element (only containing all the elements in the specified set is included)
Boolean isEmpty (): determines whether the set is empty (the data element of the set is empty)
4) Obtain Functions
Iterator iterator (): Iterator, a set of proprietary traversal methods
5) length Function
Int size (): number of elements
6) intersection Function
Boolean retainAll (Collection c): elements existing in both sets (assuming that there are two sets A, B, and A intersection of B, the final result is saved in set A, and set B remains unchanged, the returned value indicates whether set A has changed)
7) convert the set to an array
Object [] toArray (): converts a set into an array to traverse the set.
Collection has two subinterfaces: List and Set.
2. List Set
A List set has the following features: ordered (that is, the order in which elements are stored and retrieved is the same), and stored elements can be repeated.
Special Functions of the List set:
1) add features
Void add (int index, Object element): add an element at a specified position
2) Obtain Functions
Object get (int index): obtains the elements at the specified position.
3) List iterator
ListIterator listIterator (): iterates all elements
ListIteraror listIterator (int index): iterates an element at a specified position.
4) Deletion
Object remove (int index): deletes an element based on the index and returns the deleted element.
5) Modify Functions
Object set (int index, Object element): modifies an element based on the index and returns the modified element.
The List set contains three common subclasses: ArrayList, Vector, and sorted List.
3. Subclass of the List set
Subclass features of the List set:
The underlying data structure of ArrayList is an array, which provides fast query, slow addition and deletion, Low thread security, and high efficiency.
The underlying data structure of Vector is an array, which provides fast query, slow addition and deletion, thread security, and low efficiency.
The underlying data structure of the shortlist is a linked list, which is slow in query and fast in addition and deletion. Low thread security and High Efficiency
Special Functions of Vector:
1) add features
Public void addElement (Object obj)
2) Obtain Functions
Public Object elementAt (int index)
Public Enumeration elements ()
Special Functions of the shortlist:
1) add features
Public void addFirst (Object obj)
Public void addLast (Object obj)
2) Obtain Functions
Public Object getFirst ()
Public Object getLast ()
3) Deletion
Public Object removeFirst ()
Public Object removeLast ()
4. Set
The Set has the following features: unordered (the storage and retrieval order of elements are inconsistent) and unique storage elements.
The Set has two common subclasses: HashSet and TreeSet.
5. Subclass of the Set
1) features of the HashSet class:
The HashSet class does not guarantee the Set iteration sequence. In particular, it does not ensure that the sequence remains unchanged.
Note: although the elements of the Set are unordered, as a Set, it must have its own sequence of element storage. If your order is exactly the same as its storage order, it means that it cannot be ordered. You can store more data to see the results.
The underlying data structure of the HashSet class is a hash table. The uniqueness of elements in a hash table depends on two methods: int hashCode () and boolean equals (Object obj)
Use HashSet to store the execution sequence of elements:
First, compare whether the hashCode () value is the same
Yes: continue to execute equals () and check its return value.
True: indicates that the elements are repeated and are not added.
Is false: add it directly to the set.
No: add it directly to the set.
The HashSet class has a subclass of LinkedHashSet. The elements stored in LinkedHashSet are ordered and unique.
The underlying data structure of LinkedHashSet consists of a linked list and a hash table. The linked list ensures that elements are ordered and the hash table ensures that elements are unique.
2) features of the TreeSet class
The underlying data structure of the TreeSet class is a red/black tree (a self-balancing Binary Tree ).
How does the TreeSet class ensure the uniqueness of elements?
Determines whether the returned value is 0.
How does the TreeSet class ensure the sorting of elements?
A: Natural sorting (comparison of elements)
Implement the Comparable interface for the class to which the element belongs
B: comparator sorting (set comparison)
Let the set receive an implementation Class Object of Comparator