Set [Collection list, HashSet, Collection System], and define listhashset
Java. util. List (SET) interface extends Collection (SET) Interface
Features of the List set:
1. Ordered Set: the order in which elements are stored is the same as the order in which elements are retrieved (stored in: 1, 2, 3, and retrieved: 1, 2, 3)
2. Allow repeated Elements
3. Indexed
Unique Method: includes the Index Method
Add (int index, E e) add elements to the specified index
E remove (int index) remove and return, specify the elements on the index
E set (int index, E e) modify and return values to specify the elements on the index
E get (int index) returns the elements at the specified position in the list.
Note:
To operate indexes, do not overwrite the indexes; otherwise, an exception occurs.
IndexOutOfBoundsException)
StringIndexOutOfBoundsException: An exception occurred while the string index was out of bounds.
ArrayIndexOutOfBoundsException: array index out-of-bounds exception
E set (int index, E e) modify and return values to specify the elements on the index
List <Double> list
Double set (int index, E) modify and return the value to specify the element on the index
E remove (int index) remove and return, specify the elements on the index
List <Integer> list
Integer remove (int index) is removed and returned, specifying the elements on the index.
Add (int index, E e) add elements to the specified index
List <String> list
Add (int index, String e) add elements to the specified index
Java. util. Collections List set implements List Interface
The sorted list set is a two-way linked list: it ensures the iteration Order (ordered set)
The sequence list set has a large number of operations at the beginning and end. To use the special method in the sequence list, do not use polymorphism.
E removeFirst () removes and returns the first element in this list.
E pop () removes and returns the first element in this list. This method is equivalent to removeFirst ().
E removeLast () removes and returns the last element of this list.
E getFirst () returns the first element in this list.
E getLast () returns the last element of this list.
Boolean isEmpty () returns true if the list does not contain elements.
// Linked. clear ();
To prevent NoSuchElementException from element exceptions
Add a non-empty judgment before getting the element
Boolean isEmpty () returns true if the list does not contain elements.
Void addFirst (E e) inserts the specified element at the beginning of the list.
Void push (E) inserts this element into the beginning of the list. This method is equivalent to addFirst (E ).
Void addLast (E e) adds the specified element to the end of this list. Equivalent to add (E)
Data Structure Stored in List
STACK: Advanced and later
Queue: FIFO
Array: Fast query and slow addition/Deletion
Linked List: Slow query and fast addition/Deletion
Java. util. Set extends Collection Set
Set features:
1. No index, cannot use normal for Traversal
2. duplicate elements cannot be stored.
Java. util. HashSet Set implements Set
Features of a HashSet set:
1. No index, cannot use normal for Traversal
2. duplicate elements cannot be stored.
3. It is an unordered set (the order of the stored and extracted elements may be different)
4. The underlying layer is a hash table (array + linked list): Fast addition, deletion, and query
Hash Value: it is a decimal integer randomly given by the operating system.
Use the hashCode method in the Object class to obtain
Int hashCode () returns the hash code value of this object.
GetClass (). getName () + '@' + Integer. toHexString (hashCode ())
Use reflection to get the class name + @ + and convert the hashCode in decimal format to the hexadecimal value.
Override the hashCode method of the Object class
Public native int hashCode ();
It is found that there is a native on hashCode, and the method of the operating system itself is called to obtain the hash value
Use a HashSet set to store strings
The HashSet set ensures the unique element method:
1. Element rewriting hashCode Method
2. Element rewriting equals Method
String class, that is, overwrite hashCode and overwrite equals (compare whether each character of the String is the same)
Java. util. extends HashSet implements Set
LinkedHashSet set: Hash Table + linked list (hash table based on linked list)
The LinkedHashSet set is a two-way linked list that guarantees the iteration order.
Use a HashSet set to store custom types
Requirements: People of the same name and age can be stored only once.
The HashSet set ensures the unique element method:
1. Element rewriting hashCode Method
2. Element rewriting equals Method
The contains method of ArrayList determines whether elements are duplicated.
Each time an element is added, the contains method is used to determine whether the element is included in the collection.
If not
Use the HashSet set to filter repeated elements in the ArrayList set
1 private static void method_01 () {2 ArrayList <String> list = new ArrayList <String> (); 3 list. add ("a"); 4 list. add ("a"); 5 list. add ("a"); 6 list. add ("a"); 7 list. add ("a"); 8 list. add ("B"); 9 list. add ("B"); 10 list. add ("B"); 11 list. add ("B"); 12 System. out. println (list); 13 14 // create a HashSet set 15 HashSet <String> set = new HashSet <String> (); 16 // traverse the ArrayList set 17 for (String s: list) {18 // Add the element to set 19 sets. add (s); 20} 21 System. out. println (set); 22}