I. Overview of the collection
1, the origin of the collection
Java is an object-oriented language, and object-oriented language is the description of things through objects, in order to facilitate the operation of multiple objects, we have to store these multiple objects. And if we want to store multiple objects, we can't be a basic variable, but rather a variable of a container type, and in our basic knowledge of Java, what types of containers do we have? The answer is two kinds of arrays and stringbuffer, but what? The result of StringBuffer is a string that does not necessarily meet our requirements, so we can only select an array, which is an array of objects. An array of objects does not adapt to changing requirements because the length of the array is fixed. This time, in order to adapt to the needs of change, Java provides a collection of classes for us to use.
2. Differences between sets and arrays
Now that we've mentioned arrays and collections, let's say the difference between sets and arrays:
A: Length Difference
Fixed length of array
Variable length of collection
B: Different content
Arrays store elements of the same type
Collections can store different types of elements
C: Different data types for storage elements
Arrays can store basic data types, or they can store reference data types
Collection can store only reference data types
Ii. Framework System of collection set
1. Collection Collection System
The collection collection is the top-level interface for a single column collection. Collection represents a set of objects, also known as collection elements. The data structure of the collection, which is simply remembered as the way in which it is stored, is valid for the collection element.
Overview of features of the Collectoin collection:
1) Add features
Boolean Add (Object obj): Add feature
Boolean AddAll (Collection C): Add an element of a collection
2) Delete function
void Clear (): Remove all elements
Boolean remove (Object obj): Removing an element
Boolean RemoveAll (Collection c): Removes the element of a collection (as long as one element is removed to return true)
3) Judging function
Boolean contains (Object obj): Determines whether the specified element is contained in the collection
Boolean containsall (Collection C): Determines whether the collection contains the specified collection element (only the elements contained in the specified collection are called inclusions)
Boolean IsEmpty (): Determines whether the collection is empty (refers to the collection's data element is empty)
4) Get Features
Iterator Iterator (): Iterator, collection-specific traversal mode
5) Length function
int size (): Number of elements
6) Intersection function
Boolean retainall (Collection C): Two elements in the set (assuming that there are two sets a, b,a the intersection of B, the final result is saved in the collection A, the set B is unchanged, the return value represents whether the collection A has changed)
7) Convert the set to an array
Object[] ToArray (): Sets the set to the array, you can implement the collection of traversal
The collection collection has two sub-interfaces list and set.
2. List Collection
List Collection features: Ordered (refers to the order in which elements are stored and taken out), and storage elements can be duplicated
Unique features of the list collection:
1) Add features
void Add (int index,object Element): add element at specified position
2) Get Features
Object get (int index): Gets the element at the specified position
3) List iterator
Listiterator listiterator (): Iterate over all the elements
Listiteraror listiterator (int index): Iterates over elements from a specified position
4) Delete function
Object Remove (int index): Deletes elements based on index, returns deleted elements
5) Modify function
Object set (int index,object element): Modifies the element according to the index, returning the modified element
The list collection has three common subclasses of ArrayList, Vector, and LinkedList.
3. Sub-class of list collection
Sub-class features of the list collection:
ArrayList's underlying data structures are arrays, fast queries, additions and deletions, thread insecurity, high efficiency
Vector's underlying data structures are arrays, fast queries, additions and deletions, thread safety, low efficiency
LinkedList the underlying data structure is linked list, query slow, and delete quickly. Thread insecure, high efficiency
Unique features of vector:
1) Add features
public void addelement (Object obj)
2) Get Features
Public Object elementat (int index)
Public enumeration elements ()
Unique features of LinkedList:
1) Add features
public void AddFirst (Object obj)
public void AddLast (Object obj)
2) Get Features
Public Object GetFirst ()
Public Object GetLast ()
3) Delete function
Public Object Removefirst ()
Public Object Removelast ()
4. Set Set
Set Set Features: unordered (refers to the element's storage and extraction order inconsistent), storage element unique
The set set has HashSet and TreeSet two common subclasses.
5. Sub-class of set set
1) features of the HashSet class:
The HashSet class does not guarantee the set iteration order, especially because it does not guarantee that the order is constant.
Note: Although the elements of the set collection are unordered, as a collection, it must have its own order of elements stored. And if your order is exactly the same as the order in which it is stored, it doesn't mean that you can store more data and see the effect.
The underlying data structure of the HashSet class is a hash table. A hash table guarantees that the uniqueness of the element depends on two methods: int Hashcode () and Boolean equals (Object obj)
The order in which elements are executed with HashSet storage:
First compare the hashcode () values
Yes: Continue with equals () to see its return value
Is true: The description element is duplicated, not added
is false: It is added directly to the collection
No: Add directly to the collection
The HashSet class has a subclass of Linkedhashset,linkedhashset storage elements that are characterized by an orderly, unique
Linkedhashset's underlying data structure consists of a list of linked tables and a hash table, which ensures that the elements are ordered and that the elements are unique by a hash table.
2) Characteristics of the TreeSet class
The underlying data structure of the TreeSet class is a red-black tree (a self-balancing two-fork tree).
How does the TreeSet class guarantee element uniqueness?
Depends on whether the return value of the comparison is
How does the TreeSet class guarantee the ordering of elements?
A: Natural sorting (elements are comparative)
Implementing the comparable interface for the class to which the element belongs
B: Comparator sorting (collection with comparison)
Let the collection receive a comparator implementation class object
Collection Collection Summary