1: Common data structures
Stack: first in, last out
Queue: first in first out
Array: fast query, slow addition and deletion
Linked list: slow query, fast addition and deletion (comparing arrays) should start from the head
The node contains data and the address of the next node
Head node: head + ^: the node points to an empty address, indicating the end
2: Characteristics of List set subclass
Common subclasses: ArrayList, LinkedList
Arraylist: fast query, slow addition and deletion; LinkedList: slow query, fast addition and deletion
Use ArrayList and LinkedList to store characters and traverse
public class listDemo{ public static void main(String[] args){ //Create collection object ArrayList<String> array = new ArrayList<String>(); array.add("hello"); array.add("world"); //Traversal (iterator, normal for, enhanced for) for(String s : array){ System.out.println(s); } LinkedList<String> linkedList = new ArrayList<String>(); linkedList.add("hello"); linkedList.add("world"); for(String s : linkedList){ System.out.println(s); } } }
3: Three traversal modes:
Iterators: Collection specific traversal
Normal for: traversal with index
Enhanced for: the most convenient traversal method
//iterator Iterator<Student> it = array. iterator(); while (it .hasNext()){ Student s = it.next(); System.out.println(s. getName() + "," + s.getAge()); } System.out.println--------"); //Ordinary for for(int i=0; i<array.size(); i++) { Student s = array.get(i); System.out.println(s. getName() + "," + s.getAge()); } System.out.println--------"); //Enhanced for for(Student s : array) { System.out.printin(s.getName() + "," + s.getAge()); }
4: Features unique to the LinkedList collection
Public void addfirst (E) inserts the specified element at the beginning of the list
Public void addlast (E) appends the specified element to the end of this list
public E getFirst() returns the first element in this list
public E getLast() returns the last element in this list
public E removeFirst() removes from this list and returns the first element
public E removeLast() removes from this list and returns the last element
LinkedList.addFirst("hello"); LinkedList.addLast("hello"); LinkedList.getFirst( ); LinkedList.getLast( ); LinkedList.removeFirst( ); LinkedList.removeLast( );
5:set set
characteristic:
Does not contain duplicate elements and cannot be traversed with a normal for loop
Create a collection object and traverse:
HashSet: the iteration order of the set is not guaranteed
Set<String> set = new HashSet<String>(); set.add("hello"); //ergodic for(String s : set){ System.out.println(s); }
Hash value:
A numeric value of type int calculated from the address or string or number of an object
Get hash value:
public int hashCode();
Student s1 = new Student(name:"jake",age:"12"); int s=s1.hashCode(); System.out.println(s);
//The hash value returned by calling hashCode () multiple times for the same object is the same
//By default, the hash values of different objects are different. You can rewrite the method to make the hash values the same
6: HashSet set features:
·The underlying data structure is a hash table
·The iterative order of the set is not guaranteed
·Does not contain duplicate elements and cannot be traversed with a normal for loop
HashSet creates a collection object and traverses:
HashSet<String> set = new HashSet<String>(); set.add("hello"); //ergodic for(String s : set){ System.out.println(s); }
LinkedHashSet collection
·The set interface implemented by hash value and linked list has a predictable iterative order
·The storage and retrieval order of elements are consistent
·No duplicate elements
LinkedHashSet creates a collection object and traverses:
LinkedHashSet<String> set = new LinkedHashSet<String>(); set.add("hello"); //ergodic for(String s : set){ System.out.println(s); }
7: TreeSet set features:
·Does not contain duplicate elements
·Normal for loop traversal is not allowed
·The elements are ordered, depending on the construction method
TreeSet( ); Sort according to the natural sorting of elements, from small to large
TreeSet (comparator): sort according to the specified comparator
TreeSet stores integers and traverses:
TreeSet<Integer> set = new TreeSet<Integer>(); set.add(14); set.add(34); //ergodic for(Integer i : set){ System.out.println(i); }
Use of natural sorting comparable
//Create student class public class Student implements Comparable<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; } @Override public int compareTo(Student s){ return 0;//Return 1 to ascending order, return - 1 to descending order } }
public class TreeSetDemo { public. static void main(String[] args) { //Create collection object TreeSet<Student> ts = new TreeSet<Student>(); //Create student object Student s1 = new Student( name: "Xiao Ming", age: 29) ; Student s2 = new Student( name: "Xiao Hong", age: 28); Student s3 = new Student( name: "Xiaolan", age: 30); Student s4 = new Student( name: "Xiao Fang", age: 33); ts.add(s1); ts.add(s2); ts. add(s3); ts. add(s4); //ergodic for (Student s : ts) { System. out . print1n(s. getName() + "," + s.getAge()); } } }
Use of comparator:
TreeSet<Student> ts = new TreeSet<Student>(new Comparator<Student>(){ public int compare(Student s1,Student s2){ int num = s1.getAge()- s2.getAge(); int num2 =num == 0 ? s1.getName().compareTo( s2.getName()) : num; return num2; } }); //Create student object Student s1 = new Student( name: "xishi", age: 29) ; Student s2 = new Student( name: "wangzhaojun", age: 28); ts.add(s1); ts.add(s2); //ergodic for (Student s : ts) { System. out . print1n(s. getName() + "," + s.getAge()); }
8: Generic class:
Definition format: modifier class class name < type > {}
public class Generic {} common parameters in the form of T K E V are often used to represent generics
public class Generic<T>{ private T t; public T getT() { return t; } public void setT(T t) { this.t = t; } } Generic<String> g1 = new Generic<String>(); g1.setT("lily"); System.out.println(g1.getT()); Generic<Intrger> g2 = new Generic<Intrger>(); g2.setT(30); System.out.println(g2.getT());
Generic methods:
Format: modifier < type > return value type method name (type variable name) {}
Example: public void show (T) {}
generic interface
Format: modifier interface interface name < type > {}
Example: public interface Generic {}
Type wildcard:
●<?>
●List<?>: Represents a list whose element type is unknown, and its elements can match any type
● this List with wildcards only indicates that it is the parent class of various generic lists, and elements cannot be added to it
● upper limit of type wildcard: <? Extensions type >
●List<? Extensions Number >: the type it represents is Number or its subtype
● type wildcard lower limit: <? Super type >
●List<? Super Number >: the type it represents is Number or its parent type
//Type wildcard: <? > List<?> list1 = new ArrayList<object>(); List<?> list2 = new ArrayList <Number>(); List<?> list3 = new ArrayList<Integer>(); System. out . printl1n(------"); //Upper limit of type wildcard: <? Extensions type > List<? extends Number> list5 = new ArrayL ist<Number>(); List<? extends Number> list6 = new ArrayList<Integer>(); System. out . print1-(------"); //Type wildcard lower limit: <? Super type > List<? super Number> list7 = new ArrayList<object>(); List<? super Number> list8 = new ArrayList<Number>();
Variable parameters:
Format: modifier return value type method name (data type... Variable name) {}
example:
public static int sum(int... A) {} / / A is an array
public static int sum(int b,int... a) {} / / variable parameters are placed last
System. out . println(sum( ...a: 10, 20, 30, 40, 50, 60 )); System. out . println(sum( ...a: 10, 20, 30, 40, 50, 60, 70 )); public static int sum(int... a) { int sum = 0; for(int i : a) { sum += i; } return sum; }
Use of variable parameters
There is a static method in the Arrays class:
● public static List aslist(... a): returns a list of fixed sizes supported by the specified array
● the returned set cannot be added or deleted, but can be modified
*There is a static method in the List interface:*
● public static List of(E... elements): returns an immutable list containing any number of elements
● the returned set cannot be added, deleted or modified
There is a static method in the Set interface:
● public static Set of(... elements): Return - an immutable set containing any number of elements
● do not give duplicate elements
● the returned set cannot be added or deleted, and there is no modification method