Collections class
java.utils.Collections is a collection of tools for the collection operation.
Collections class of common methods
Here are four common methods:
addAll(Collection<T> c, T ... elements)
shuffle(List<?> list)
sort(List<r> list)
sort(List<T> list, Comparator<? super T>)
public static <T> Boolean adda1l (Collection <T> C, T ... Elements) add some elements to the collection. public static void shuff1e (<?> List 1ist) out of order: disrupt the order of collection. public static <T> void Sort (List <R & lt> 1ist): The elements in the set according to the default ordering rules. public static <T> void the Sort (List <T> List, Comparator <? Super T>): The elements in the collection sorted according to the specified rules.
addAll(Collection<T> c, T ... elements)方法
Parameter Description:
Collection <T> c: is the set of elements to be added
T ... elements: a variable parameter, is a collection of elements to give added
Method features:
Add some elements to the collection
example:
Import java.util.Collections; Import java.util.HashSet with; public class DemoCollections { public static void main (String [] args) { // Create a set HashSet HashSet <String> = hashSet new new HashSet <> (); // collections class static method call addAll, added to the set of a plurality of elements hashSet Collections.addAll (hashSet, "a", "B", "C", "D", "E", "F", "G" ) ; // output HashSet set System.out.println (hashSet); } }
Output Results:
[A, B, C, D, E, F, G]
shuffle(List<?> list)方法
Parameter Description:
List list <?>: Means that the incoming collection, that is, the method is a set of parameters passed
Method features:
Scrambled collection.
example:
Import of java.util.ArrayList; Import java.util.Collections; public class DemoCollectionsShuffle { public static void main (String [] args) { // Create a set ArrayList ArrayList <String> = the arrayList new new ArrayList <> (); // arrayList additive element to set Collections.addAll (arrayList, "A", "B", "C", "D", "E", "F.", "G" ); System.out.println ( "no play when the chaos set: "+ arrayList); // call the static method shuffle collections class, set the order of the elements which disrupted arrayList Collections.shuffle (arrayList); System.out.println("Disrupted after collection:" + the arrayList); } }
Output Results:
set when there is no upset: [A, B, C, D, E, F, G]
after disrupted set: [D, B, C, E, A, G, F]
sort(List<r> list)方法
Parameter Description:
List list <?>: Means that the incoming collection, that is, the method is a set of parameters passed
Method features:
Set to be sorted (ascending order by default)
example:
Import of java.util.ArrayList; Import java.util.Collections; public class Demo01CollectionsSort { public static void main (String [] args) { // Create a set ArrayList ArrayList <Integer> = the arrayList new new ArrayList <> (); // arrayList added to the collection of elements Collections.addAll (arrayList,. 1, 2, 10,. 9,. 8 ); System.out.println ( "set at not sorted:" + arrayList); // call to sort () method, the arrayList set sorting elements inside the Collections.sort (arrayList); (System.out.println "sorted collection:" + arrayList); } }
Output Results: set at not sorted: [ 1, 2, 10, 9, 8 ] set sorted: [ 1, 2, 8, 9, 10]
sort(List<T> list, Comparator<? super T>)方法
First of all, explain, this method can only do understand, the learning process using the sort (List <T> list) are usually the default method.
Parameter Description:
List list <?>: Means that the incoming collection, that is the first argument passed to the method is a collection of
Comparator <? Super T>: This is a collation, a custom (sorted overwrite the interface)
Method features:
Set to be sorted (ascending order by default)
example:
// 随便创建一个Person类 public class Person { private String name; private int age; public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", 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; } }
Their definition of a collation for testing:
// Example Import of java.util.ArrayList; Import java.util.Collections; Import java.util.Comparator; public class Demo02CollectionsSort { public static void main (String [] args) { // Create a set ArrayList ArrayList <Person> arrayList = new new the arrayList <> (); // add elements to arrayList set, the set of types of data is stored in a custom type (the Person) Collections.addAll ( arrayList, new new the Person ( "LeeHua", 20 is ), new new the Person ( " WanTao ", 18 ), new newThe Person ( "XiaMin", 22 is ), new new the Person ( "HonMao", 22 is ) ); System.out.println ( "set at not sorted:" + arrayList); // custom collation, sorting collections of arrayList the Collections.sort (the arrayList, new new Comparator <the Person> () { @Override public int Compare (the Person O1, O2 the Person) { // Age equal, sorted by name the while (o1.getAge () - o2.getAge () == 0 ) { IF (o1.getName (). length ()> o2.getName (). length ()) { for ( int i = 0; i < o2.getName().length(); i++) { if (o1.getName().charAt(i) - o2.getName().charAt(i) != 0 ) { return o1.getName().charAt(i) - o2.getName().charAt(i); } } return 1; } else if (o1.getName().length() < o2.getName().length()) { for (int i = 0; i < o1.getName().length(); i++) { if (o1.getName().charAt(i) - o2.getName().charAt(i) != 0 ) { return o1.getName().charAt(i) - o2.getName().charAt(i); } } return - 1; } else { for (int i = 0; i < o2.getName().length(); i++) { if (o1.getName().charAt(i) - o2.getName().charAt(i) != 0 ) { return o1.getName().charAt(i) - o2.getName().charAt(i); } } return 0; } } // age is not equal, sorted Age return o1.getAge () - o2.getAge (); } }); System.out.println ( "sorting after collection:" + the arrayList); } }
Output Results: set at not sorted: [the Person {name = 'LeeHua', Age = 20 is}, the Person {name = 'WanTao', Age = 18 is}, the Person {name = 'XiaMin', Age = 22 is}, the Person {22 is name = 'HonMao', Age = }] set after sorting: [the Person {name = 'WanTao', Age = 18 is}, the Person {name = 'LeeHua', Age = 20 is}, the Person {name = 'HonMao ', age = 22}, Person {name =' XiaMin ', age = 22}]
Collation code to understand:
new new Comparator <the Person> () { @Override public int Compare (the Person O1, O2 the Person) { // is first determined whether the age is equal to the while (o1.getAge () - o2.getAge () == 0 ) { // If this Age Age == object, then press the name of the letter (char code) comparing // for each letter of the name to compare IF (o1.getName (). length ()> o2.getName (). length ()) { // O1 name length> o2 name length for ( int I = 0; I <o2.getName () length ();. I ++ ) { IF . (o1.getName () the charAt (I) - o2.getName ( ) .charAt (i)! = 0) { Return . O1.getName () the charAt (I) - . O2.getName () the charAt (I); } } // if all letters of the name o2, are equal to the sequence o1 bits corresponding to the letters of the name , the length of the large letter row, i.e. the row o1 return . 1 ; } // the name of length o1 <o2 name length the else IF (. o1.getName () length () < . o2.getName () length ()) { for ( int I = 0; I <o1.getName () length ();. I ++ ) { IF (. o1.getName () the charAt (I) - o2.getName () the charAt (I) = 0.! ) { return. o1.getName () the charAt (I) - . o2.getName () the charAt (I); } } // if all letters of the name o1, o2 order are equal to the number of bits corresponding to the letters of the name, the length of the letter after the large discharge, i.e. after the exhaust o2 return -. 1 ; } // the name of length o1 o2 = length of the name of the else { for ( int I = 0; I <o2.getName () length (); I ++. ) { IF (..! o1.getName () the charAt (I) - o2.getName () the charAt (I) = 0 ) { return o1.getName () the charAt (I) -. o2.getName () the charAt (I).; } } //If all the letters of the name o1, o2 order are equal to the number of bits corresponding to the letters of the name, then sorted in order of addition arrayList set return 0 ; } } // age is not equal, sorting by age return o1.getAge () - o2.getAge (); } }