/*collections is the collection tool class. The two common methods in the tool class are: 1,sort (list), sort (list, custom comparer) 2,max (list), Max (list, custom comparer) 3,binarysearch (List,key)// Using the binary lookup method to find the index of the key element in the list, there is a return index, no return-(insertion point) -1*/import java.util.*;class collectionsdemo {public static void main ( String[] args) {Sortdemo ();} public static void Sortdemo () {list<string> list=new arraylist<string> (); List.add ("Shdgjh"); List.add (" ALKLKJJJ "), List.add (" and "), List.add (" Hello "), List.add (" World "), List.add (" Hello "), List.add (" Hello ");//sop (" Original list: "+list";//Sort: collections.sort (list);//Sort the specified list in ascending order based on the natural ordering of the elements. SOP ("List after natural order:" +list);//collections.sort (List,new mycomparator ());//Sorts the specified list according to the order produced by the specified comparer. SOP ("list after string length:" +list),//Gets the maximum value//string Max=collections.max (list), or returns the largest element of a given collection based on the natural order of the element. SOP ("Returns the most natural order of objects:" +max);//string Max1=collections.max (List,new mycomparator ());//sop ("returns the object with the largest string length:" +max1); int Num=collections.binarysearch (list, "and");//Use binary lookup to find the index value of an element and return the SOP (Index of "and-call BinarySearch method:" +num); int y= HalfsearCH (list, "and"), SOP (Index of "and"-Call custom Halfsearch method: "+y);} public static void Sop (Object obj) {System.out.println (obj);} public static int Halfsearch (list<string> list,string key) {int min=0,max=list.size () -1;while (min<=max) {int Mid= (Min+max) >>1;//left by right except string Str=list.get (mid); int X=str.compareto (key); if (x>0) max=mid-1;else if (x<0 ) Min=mid+1;elsereturn mid;} return-min-1;}} Class Mycomparator implements Comparator<string>//Specifies the comparer, sorted by string length {public int compare (string s1,string s2) {if ( S1.length () >s2.length ()) Return 1;if (S1.length () <s2.length ()) Return-1;return S1.compareto (s2);}}
Tool classes for the/*collection collection: Common Methods in Collections (cont.) 1,fill (list, "Hello"), and//Replace all objects in the list with Hello. 2,replaceall (list,old,new);//Replace all old objects in list with the new object. 3,reverse (list);//list element inversion */import java.util.*;class CollectionsDemo1 {public static void main (string[] args) { Filldemo (); }public static void Filldemo () {list<string> list=new arraylist<string> (); List.add ("Hello1"); List.add (" Hello2 "), List.add (" Hello3 "), List.add (" Hello4 "), List.add (" Hello5 "), List.add (" Hello6 "), List.add (" Hello3 "), SOP ( list); Collections.shuffle (list); SOP (list); Collections.swap (list,1,3), SOP (list),//collections.fill (list, "Hello World"),//Replace all objects in list//list=filldemo_1 (1,4 , list, "Me");//Replace the object that specifies the starting and ending positions of the index. Collections.replaceall (list, "Hello3", "Me ...");//Replace All Hello3 object SOP (list) in list; Collections.reverse (list);//reverse SOP (list);} public static list<string> Filldemo_1 (int. first,int last,list<string> list,string key) {for (int i=first;i <last;i++) {list.set (i,key);} return list;} public static void Sop (Object obj) {System.out.println (obj);}}
/* There is a reversal method in the tool class collections, and a comparer is returned. Reverseorder () returns a comparer that forcibly reverses the natural order of collection objects that implement the comparable interface. In this case, the TreeSet collection will sort the elements in a natural order, and if the method in the tool class is called in the constructor, the natural order is forcibly reversed. Collections.reverseoreder ();//reverse the natural order. Collections.reverseoreder (New Mycomparator ());//reverses the order in which the custom comparator is compared. The */import java.util.*;//code implements the Reverseorder () inversion method of the Toolset: this part of the code is not written in real-world development. Because it's already packaged class Strcomparator implements Comparator<string>{public int compare (String s1,string s2) {return S2.compareto (S1);//reverse of natural order. return S1.compareto (S2);//positive sequence of natural order. /* Reverse order Explanation: int Num=s1.compareto (s2), if (num>0) return-1;if (num<0) return 1;return Num;*/}}class Mycomparator Implements Comparator<string>//Specifies the comparer, sorted by string length, from short to long {public int compare (string s1,string s2) {if (S1.length () >s2.length ()) Return 1;if (S1.length () <s2.length ()) Return-1;return S1.compareto (s2);}} Class CollectionsDemo2 {public static void main (string[] args) {//treeset<string> ts=new treeset<string> ( Collections.reverseorder ());//Use the Inversion method in the tool class (natural order)//treeset< String> ts=new treeset<string> (New Strcomparator ());//Natural Order reversal principle implementation treeset<string> Ts=new TreeSet< String> (Collections.reverseorder (New Mycomparator ()));//reverse the order in which the custom comparator is compared, this is really good cow! Ts.add ("Hello"), Ts.add ("KL"), Ts.add ("Heyjk"), Ts.add ("Ash"), Ts.add ("U"); Ts.add ("Bght"); SOP (TS);iterator< String> It=ts.iterator (); while (It.hasnext ()) {SOP (It.next ());}} public static void Sop (Object obj) {System.out.println (obj);}}
Java Collections Collection Tool class