25. Collections class:
Collections is a tool class used to operate Collections. It mainly provides sorting algorithms, including random sorting and reverse sorting.
The Collections class provides some static methods to implement some common algorithms based on List containers.
A list of Collections methods:
· Void sort (List): sorts the elements in the List.
· Void shuffle (List): random sorting of elements in the List.
· Void reverse (List): sorts the elements in the List in reverse order.
· Void fill (List, Object): rewrite the List container with a specific Object. It only copies the reference of the same Object to every location of the container, and the support is valid for the List.
· Void copy (List dest, List src): copy the content in src to destiny.
· Int binarySearch (List, Object): returns the position of the List in the binary method.
The following is an example of Collections:
Import java. util. arrayList; import java. util. arrays; import java. util. collections; import java. util. iterator; import java. util. list; public class CollectionsTest {@ SuppressWarnings ({"rawtypes", "unchecked"}) public static void main (String [] args) {List list List = new ArrayList (); // instantiate the Student object Student stu1 = new Student (18, "zhao", 87); Student stu2 = new Student (23, "qian", 80 ); student stu3 = new Student (2 6, "sun", 92); Student stu4 = new Student (25, "li", 76); // Add the Student object to the List container list. add (stu1); list. add (stu2); list. add (stu3); list. add (stu4); System. out. println ("Contents of the initialization List:"); printView (list); Collections. shuffle (list); System. out. println ("Contents of the List after random sorting:"); printView (list); Collections. sort (list); System. out. println ("sorted List content:"); printView (list); Collections. reverse (list); System. out. println ("reverse List content:"); pr IntView (list);/*** when using the copy () method in the Collections class, you must initialize the List corresponding to the dest and fill it with the object. Arrays. the lasList () method is used to compile the Object class * Into a List queue */List newList = new ArrayList (Arrays. asList (new Object [list. size ()]); Collections. copy (newList, list); System. out. println ("Copy list content:"); printView (newList);} // display method public static void printView (ListArrayList) {IteratorIt = arrayList. iterator (); while (it. hasNext () {System. out. print ("" + it. next ();} System. out. println ();}}
Student class code:
@ SuppressWarnings ("rawtypes") public class Student implements Comparable {// declare the attribute private Integer age; private String name; private Integer score; // constructor ...... // Get, set method ...... // Override the toString method public String toString () {return "name =" + name + ", age =" + age + ", score" + score + "\ n ";} // override the compareTo method public int compareTo (Object o) {Student s = (Student) o; if (s. getScore (). compareTo (score)> 0) return-1; else if (s. getScore (). compareTo (score) <0) return 1; elsereturn 0 ;}}
Output result:
Initialize the List content:
Name = zhao, age = 18, score 87
Name = qian, age = 23, score 80
Name = sun, age = 26, score 92
Name = li, age = 25, score 76
Contents of the List after random sorting:
Name = qian, age = 23, score 80
Name = zhao, age = 18, score 87
Name = li, age = 25, score 76
Name = sun, age = 26, score 92
Content of the sorted List:
Name = li, age = 25, score 76
Name = qian, age = 23, score 80
Name = zhao, age = 18, score 87
Name = sun, age = 26, score 92
List content in reverse order:
Name = sun, age = 26, score 92
Name = zhao, age = 18, score 87
Name = qian, age = 23, score 80
Name = li, age = 25, score 76
Copy the list content:
Name = sun, age = 26, score 92
Name = zhao, age = 18, score 87
Name = qian, age = 23, score 80
Name = li, age = 25, score 76
Container selection criteria:
1> storage requirements:
Unordered: Set, which cannot be repeated.
Ordered: List, which can be repeated.
Key-value Pair: Map.
2> read/write efficiency:
Hash: reads, writes, and changes are fast.
Array: fast reading/writing and slow modification.
Linked: Slow read/write and fast change.
Tree: elements can be sorted.