Java single column set summary, java single column set
Collection Interface
Add ()
Remove ()
Contains ()
Clear ();
Size ();
Iterator traversal (a common iterator cannot modify the length of a set during traversal)
List interface single column set ordered and repeatable Index
Add (index, obj)
Remove (index)
Get (index)
Set (index, obj)
List Traversal
For Loop
Iterator
Procedure
Create a collection object
Create element objects (reference data types (including basic data type packaging objects ))
Call the Set Method to add, delete, modify, and query
Use the iterator to traverse the set. (The ListIterator list iterator can modify the length of the set during the traversal process)
HashNext check whether there is any next element
If any: Call the next method to obtain
Two sons, Vector (learning by themselves using API Documentation)
Thread unsafe: efficient
ArrayList: The underlying layer is an array structure, which provides fast query and slow addition and deletion.
Slow list: underlying linked list structure, slow query, fast addition and Deletion
Who should I use?
Multiple queries: ArrayList
Add, delete, and more: Upload list
If you don't know who to use it, use ArrayList.
Unordered Set of single columns in the Set interface cannot be repeated.
Unique and unordered elements (the storage and read order may be inconsistent)
HashSet
How to ensure unique element? The underlying data structure is a hash table, which depends on the hash value of elements.
Final: hashcode method and equals Method
Sequence:
First, compare the return values of hashcode.
Same: continue to call the equals method to compare the content of the element
Different: add to the set directly
HashSet stores basic data types and strings: because of the basic data amount type packaging class and the String rewriting hashCode and equals Methods
Custom type of HashSet storage: The hashcode and equals methods need to be rewritten.
TreeSet
How to ensure unique element?
Is the return value 0 based on the compareTo method?
How to sort Elements
1. The class where the element is located implements the Comparable interface and overrides the compareTo method.
2. The set is comparable. When a TreeSet set object is created, a constructor is called to pass in the comparator object (anonymous internal class)
Traversal set: iterator and enhanced for Loop
LinkedHashSet
The underlying layer is the hash table and linked list.
Ensure the uniqueness and order of Elements
Enhance the for loop !!!!
Set Traversal
For (element type variable name: Set Name ){
Convert objects to subclass objects and use special members of subclass !!!!!!
The output variable name is each element in the set.
}