catalogue
1: List
1.1 overview and characteristics of list set:
List collection overview:
- An ordered set (also known as a sequence), in which the user can accurately control the insertion position of each element in the list. Users can access elements through integer indexes and search for elements in the list.
- Unlike set sets, lists usually allow duplicate sets.
List set features:
- Ordered: the stored and retrieved elements are in the same order.
- Repeatable: stored elements can be repeated.
-
import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Main{ public static void main(String[] args) { List<String> list=new ArrayList<String>(); list.add("hello"); list.add("world"); list.add("java"); //Iterator traversal Iterator<String> iterator = list.iterator(); while(iterator.hasNext()){ String s=iterator.next(); System.out.println(s); } } }
1.2 List set specific methods
1.3 concurrent modificationexception
Cause: in the process of iterator traversal, the length of the elements in the set is modified through the set object, resulting in the inconsistency between the expected modified value and the actual modified value judged by the iterator in obtaining the elements.
Solution: use the for loop to traverse, and then use the collection object to do the corresponding operation.
1.4 ListIterator: list iterator
- It is obtained through the listIterator () method of the List set, so it is an iterator of the List set.
- A list iterator that allows the programmer to traverse the list in either direction, modify the list during the iteration, and get the current position of the iterator in the list.
import java.util.ArrayList; import java.util.List; import java.util.ListIterator; public class Main { public static void main(String[] args) { List<String> list=new ArrayList<>(); list.add("hello"); list.add("world"); list.add("java"); ListIterator<String> li=list.listIterator(); while(li.hasNext()){//forward traversal String s=li.next(); System.out.println(s); } System.out.println("----------"); while(li.hasPrevious()){//Backward Traversal String s=li.previous(); System.out.println(s); } System.out.println("----------"); ListIterator<String> li1=list.listIterator(); while(li1.hasNext()){ String s=li1.next(); if(s.equals("world")){ li1.add("javaee"); } } System.out.println(list); } }
1.5 enhanced for loop
Enhanced for: simplifies traversal of arrays and collections.
- Classes that implement the iterator interface allow their objects to be the target of enhanced for statements.
- It appeared after JDK5, and its internal principle is an Iterator iterator.
import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { int[] arr={1,2,3,4,5,6}; for(int i : arr){ System.out.println(i); } System.out.println("----------"); String[] brr={"hello","world","java"}; for(String s:brr){ System.out.println(s); } System.out.println("------------"); List<String> list=new ArrayList<>(); list.add("hello"); list.add("world"); list.add("java"); //The internal principle of the for enhancement is an Iterator iterator /*for(String s:list){//Concurrent exception modification will be thrown. if(s.equals("world")){ list.add("javaee"); } }*/ }
1.6 characteristics of list aggregation subclass:
Common subclasses of List collection: ArrayList, LinkedList
- ArrayList: the underlying data structure is an array, which is fast to query and slow to add or delete.
- LinkedList: the underlying data structure is linked list, which is slow to query and fast to add and delete.
Case: the ArrayList set stores student objects and traverses them in three ways
import java.util.ArrayList; import java.util.Iterator; public class Main { public static void main(String[] args) { //Create an ArrayList collection object ArrayList<student> array = new ArrayList<>(); //Define student objects student s1 = new student("Xiao Ming", 18); student s2 = new student("floret", 19); student s3 = new student("Xiao Zhou", 20); //Store student objects array.add(s1); array.add(s2); array.add(s3); //Traverse the collection in three ways //1. Iterator: a unique traversal method of a collection Iterator<student> it = array.iterator(); while (it.hasNext()) { student s = it.next(); System.out.println(s.getName() + "," + s.getAge()); } System.out.println("---------------"); //2.for enhancements for(student s:array){ System.out.println(s.getName() + "," + s.getAge()); } System.out.println("---------------"); //3. Ordinary for loop for(int i=0;i<array.size();i++){ student s=array.get(i); System.out.println(s.getName() + "," + s.getAge()); } } } class student { private String name; private int age; public student() { } public student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
1.7 unique functions of LinkedList set
II: set
2.1 set features:
- A collection that does not contain duplicate elements
- There is no index method, so ordinary for loop traversal cannot be used
import java.util.Set; import java.util.HashSet; public class Main { public static void main(String[] args) { //Create collection object Set<String> set=new HashSet<>(); //Add element set.add("hello"); set.add("world"); set.add("java"); set.add("javaee"); set.add("world"); System.out.println(set);//[world, java, hello] does not contain duplicate elements //HashSet: there is no guarantee for the iterative order of the set } }
2.2 Hash value
Hash value: it is a value of int type calculated by JDK according to the address, string or number of the object.
There is a method in the Object class that can get the hash value of the Object.
public int hashCode(): returns the hash code value of the object
Hash value characteristics of object
- The hash value of the hashCode () method called by the same object multiple times is the same
- By default, different objects have different hash values. The double write hashCode () method can make the hash values of different objects the same.
public class Main { public static void main(String[] args) { //Create student object student s1=new student("millet",18); //The hashCode () method is called multiple times for the same object, and the hash value returned is the same System.out.println(s1.hashCode());//1324119927 System.out.println(s1.hashCode());//1324119927 student s2=new student("Xiao Gang",19); //By default, different hash values are different //Through method rewriting, you can realize that the hash values of different objects are the same System.out.println(s2.hashCode());//990368553 System.out.println("---------------"); System.out.println("hello".hashCode());//99162322 System.out.println("world".hashCode());//113318802 System.out.println("java".hashCode());//3254818 System.out.println("Heavily".hashCode());//1179395 System.out.println("conversation".hashCode());//1179395 } } class student{ private String name; private int age; public student() { } public student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
2.3 HashSet set overview and features:
HashSet set features:
- The underlying data structure is a hash table
- There is no guarantee for the iterative order of the set, that is, the order of stored and extracted elements is not guaranteed
- There is no indexed method, so you can't use a normal for loop to traverse
- Because it is a set set, it is a set that does not contain duplicate elements
public class Main { public static void main(String[] args) { HashSet<String> hs=new HashSet<String>(); //Add element hs.add("hello"); hs.add("world"); hs.add("java"); hs.add("world"); //ergodic for(String s:hs){ System.out.println(s); } /*world java hello */ //There is no guarantee for the iterative order of the set, that is, the order of stored and extracted elements is not guaranteed } }
2.4 source code analysis of HashSet set to ensure element uniqueness:
The process of adding an element to the HashSet collection:
HashSet collection storage elements:
To ensure element uniqueness, you need to override hashCode () and equals ()
2.5 hash table of common data structures
Hash table:
- Between JKD8, the bottom layer is realized by array + linked list, which can be said to be an array with linked list elements
- After JKD8, when the length is long, the bottom layer is optimized