public class News implements comparable {private int id; News number private String title; News title @overridepublic String toString () {return "news [number =" + ID + ", title =" + title + "]";} Public News () {//no parameter Structure super ();} public News (int id, String title) {//Parameter structure super (); this.id = Id;this.title = title;} public int getId () {return ID;} public void setId (int id) {this.id = ID;} Public String GetTitle () {return title;} public void Settitle (String title) {this.title = title;} Override sorted rules ascending @overridepublic int compareTo (Object o) {news news= (news) O; if (News.getid () ==this.id) {return 0;} else if (News.getid () <this.id) {return 1; Descending return-1;} else {return-1;//Descending return 1;}}} public class Collectionstest {public static void main (string[] args) {/* * Operation Collection of Tool classes collections * *///Create a collection list<string > List=new arraylist<string> (), List.add ("B"), List.add ("D"), List.add ("C"), List.add ("a"); System.out.println ("no sort = =" +list); Order of insertion time//random sorting of sets Collections.shuffle (list);//Lottery number 136 order must 631 Not System.out.println ("random sort = =" "+list");//in ascending alphabetical order Collections.sort (list); System.out.println ("ascending = = =" "+list"); Descending is also required after ascending collections.reverse (list); System.out.println ("descending = = =" "+list"); The position of the query "B" in the collection must be sorted in ascending order before querying collections.sort (list); System.out.println (Collections.binarysearch (list, "a")); System.out.println ("****************************"); List<news> news=new arraylist<news> (); Want to implement the sorting of objects News.add (new News (3, "News 3")), News.add (New News (1, "News 1")), News.add (New News (4, "News 4")), News.add (New News (2, "News 2")); for (news News2:news) {System.out.println (NEWS2);} /* * If news does not rewrite the CompareTo () in the comparable interface is a compile error! * We must rewrite the CompareTo () Definition rule */collections.sort (news); System.out.println ("After object sort ..."); for (News news2:news) {System.out.println (NEWS2);}} @Testpublic void Test1 () {//by using split () in the string class to convert the string to a string array string[] str1= "A b c D D d". Split ("");//Convert an array to a set List<strin g> aslist = arrays.aslist (STR1); System.out.println (aslist); String[] str2= "C D". Split (""); List<sTring> aslist2= arrays.aslist (STR2);//last seen position System.out.println (Collections.lastindexofsublist (Aslist, ASLIST2));//first occurrence position System.out.println (Collections.indexofsublist (Aslist, AsList2));} @Testpublic void Test2 () {string[] str1= "A b c D E F". Split ("");//Convert the array to a collection list<string> list = Arrays.aslist (STR1); SYSTEM.OUT.PRINTLN (list);//The elements in the collection move backward distance positions, and then the overridden elements loop forward collections.rotate (list, 2); SYSTEM.OUT.PRINTLN (list);}}
Java Collection Framework 03