Collections: A tool class that operates exclusively on a collection. All methods are static, the construction method is private, the object cannot be created, through COLLECTIONS.XXX (), and the method in the tool class is invoked. 1, collections.sort (); Sort the collection, list can, set not, because there is an automatic sort of treeset, is actually called the Compare method of the object, if you want to sort the custom object, sort after to pass to the comparer. Collections.sort (list,new Strlen ()); Below is an example of 2,collections.max ();//The maximum value is also compared according to the Compare method. can also write collections.max (List,new Strlen ()); 3,int c =collections.binarysearch (list, "KK"); Binary lookup, return the element angle, if the element does not exist, will return a negative number, position three can also pass into the comparer, in the next call the comparator's compare method. 4,collections.fill (List, "PP");//change the elements in the collection to "PP" sop (list); 5,collections.replaceall (List,old_value, New_value);//replace all old values in the collection with the new values. 6,collections.reverse (list);//Flips the entire list 7,collections.reverseorder ();//Returns a comparator object in reverse. TreeSet<String> ts = new treeset<string> (Collections.reverseorder ()); You can also force an existing comparer to be reversed, taking the example of a custom comparer in the examples below. treeset<string> ts = new treeset<string> (Collections.reverseorder (New strlen ())); 8,list ls = Collections.synchronizedlist (List ls); Returns a thread-safe list 9,collections.swap (LIST.INDEX1,INDEX2);//Swaps two-position elements. 10,collections.shuffle (list); Randomly arranges the collection elements. arrays: The manipulate Array tool class, which contains a lot of static methods. 1,arrays.sort (l); Arrays.sort (l,from,to), there are a lot of the same as above, but to change the set to an array. 2,arrays.tostring (a);//return String 3,list arraylist.aslist (a); Changes the array to a list collection, but you cannot use the add and subtract methods of the collection. Because the length of the array is fixed. Only the reference to the array is placed in the collection. If the elements in the array are objects, then the elements in the array are transferred directly to the elements in the collection if the elements in the array are basic data types, the array is used as the element in the collection set of sets of variables, T [] collection.toarray (t [] a); As an example, the length is best equal to the length of arr. Advanced for LoopFormat for (data type variable name: traversed collection or array) {SOP (variable name); but cannot be modified, can only be removed, principle or iterator}import java.util.*; public class Test {public static void main (string[] args) {arraylist<string> li = new arraylist<string> () ; Li.add ("ABC1"); Li.add ("ABC2"); Li.add ("ABC3"); for (String S:li) {SOP (s); }} public static void sop (Object obj) {System.out.println (obj);}}variable ParametersA variable parameter is defined at the last edge of the parameter list. Import java.util.*; public class Test {public static void main (string[] args) {show (1,2,3,5,8,6,5,5,65,6,6,6,6,7);} public static void Show (int ... arr) {for (int x=0;x<arr.length;x++) SOP (Arr[x]), public static void sop (Object obj ) {System.out.println (obj);}}static pour import static java.util.arrays*; You can write the sort (array) directly, and when the name of the method is the same, you know which object or class it belongs to. Custom Comparer Example: compare string lengths. import java.util.*;p ublic class Test { public static void main (string[] args) { sort (); } public static void sop (Object obj) { System.out.println (obj); } public static void sort () { list<string> List = new arraylist<string> (); list.add ("ABCD"); List.add ("AAA"); List.add ("zzz"); list.add ("KK"); List.add ("ABCD"); List.add ("QQ"); List.add ("z"); sop (list); Collections.sort (list,new Strlen ()); SOP (list); }}class Strlen Implements Comparator<string>{ public int compare (String s1,string s2) { int num = s1.length ()- S2.length (); if (num==0) return s1.compareto (S2); else return num; }} Set Variable group: import java.util.*;p ublic class Test { public static void main (STRIng[] args) { arraylist<string> li = new arraylist<string> (); li.add ("ABC1"); Li.add ("ABC2"); li.add ("ABC3"); String arr [] = Li.toarray (new String [5]); SOP (arrays.tostring (arr)); } public static void sop (Object obj) { System.out.println (obj); }}
JAVA 16 (Collection Framework Tool Class)