この質問では、次のように、コレクションツールクラスの2つのsort()メソッドを紹介します。
1。コレクションツールクラスの2つのソート()メソッド
フォーマット1:public static <tは比較可能<?スーパーT >> voidソート(リスト<T>リスト)
注:この方法のgenerics <t>はすべて、比較可能なインターフェイスのサブクラスです。つまり、比較可能なインターフェイスサブクラス型のデータのみを比較してソートすることができます。他の種類のデータを比較してソートする場合、同等のインターフェイスを継承し、
equals()およびcompareto()メソッドをオーバーライドします。その中でも、文字列クラスと整数クラスは両方とも同等のインターフェイスサブクラスであり、ソートすることができますが、基本的なタイプはソートすることはできません。比較項目はクラス内で指定されています
フォーマット2:public static <t> voidソート(リスト<t>リスト、コンパレータ<?スーパーt> c)
注:この方法は、比較方法コンパレータ<を指定します。スーパーT> C、つまり、Cはコンパレータを実装する必要があります<?スーパーT>インターフェイス、比較項目を指定するために比較()メソッドをオーバーライドします。比較項目はクラスの外で指定されていますが、これはより柔軟です
2。例
例で文字列と数字を取得する一般的な方法:
/ ***ランダムと非繰り返しの文字列を生成:数字生成文字列数*/ public static list <string> generatestring(int number){list <string> listString = new ArrayList <>(); //返品値リスト<integer> length = nullを保存するために使用されます。 // string length stringbuffer sb = new StringBuffer(); //中間変数intコントロール= 0; // control number string [] chars = new String [] {"a"、 "b"、 "c"、 "d"、 "e"、 "f"、 "g"、 "h"、 "i"、 "j"、k "、" l "、" m "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "s"、 "t"、 "u"、 "v"、 "w"、 "x"、 "y"、 "z"}; while(true){//コントロールの終わりif(control == number){break; } //乱数を生成し、36ビット2AAAB761-4341-4968-ACEB-3861EE3824B2 UUID DATAT STRING UUID = UUID.RANDOMUID()。TOSTRING()。TOSTRING()。 sb.setlength(0); //ランダムな文字列の長さを取得すると、長さは0ではありません{length = getDiffno(1、11); } while(length.get(0)== 0); // string for(int i = 0; i <length(0); i ++){string str = uuid.substring(i*3、(i*3+3)); // str文字列を16進数に変換し、その値を取得しますint x = integer.parseint(str、16); //残りを取得:x%0x3e - 0x3e = 3*16 + 14 = 62、ここで、charsには62文字sb.append(chars [x%0x3e]); } listString.add(sb.toString());コントロール++; } return listString; }/ ***ランダムと非繰り返しの数値を生成します:n生成数値max max生成範囲*/ public static list <integer> getdiffno(int n、int max){//これらの乱数リスト<-n]リスト<n]リスト<ne <n]リスト<integer>リスト= new Arraylist <>(); RANDOM RANDOM = new Random();整数K; for(int i = 0; i <n; i ++){do {k = random.nextint(max); } while(list.contains(k)); list.add(k); }返品リスト。 } 1.整数ジェネリックのリストを並べ替えます
/*** 1。collections.sort()メソッドを介して整数ジェネリックのリストを並べ替えます。 *整数ジェネリックのリストを作成し、100以内に10個の非再検討のランダム整数を挿入し、コレクション()メソッドを並べ替えるためのメソッドを並べ替えます* 2。 System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- integer : integerList) { system.out.println( "element:" + integer); System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 2。文字列ジェネリックのリストを並べ替えます
/*** 1。文字列ジェネリックのリストを並べ替えます。文字列ジェネリックのリストを作成し、順序外文字列要素を追加し、ソートメソッドを呼び出し、ソート順序をもう一度出力します */ public void listStringsort(){list <string> stringList = new ArrayList <String>(); stringlist.add( "eipjlcx"); stringlist.add( "wvqrufc"); stringlist.add( "j"); stringlist.add( "hdau2g"); stringlist.add( "m0wswhd3"); System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- / ***文字列ジェネリックのリストを並べ替えて、10個の非再検証文字列のランダム生成を必要とし、文字列の長さは10*/ public void listStringRandomsort(){//ランダムな文字列リスト<文字列> listString = generatestring(10); System.out.println("------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println("-------------------------------------------------------------"); for (String integer : listString) { System.out.println("Element:" + integer); } }3.他のタイプのジェネリックのリストのソート
コースクラスの実装
/** * COURSE CLASS * @Author Administrator * */public class Course {public String id;パブリック文字列名;パブリックコース(文字列ID、文字列名){this.id = id; this.name = name; } public Course(){} @Override public int hashcode(){final int prime = 31; int result = 1; result = prime * result +((name == null)?0:name.hashcode());返品結果; } @Override public boolean equals(object obj){if(this == obj)return true; if(obj == null)falseを返します。 if(!(obj instanceof course))falseを返します。コースその他=(コース)obj; if(name == null){if(other.name!= null)return false; } else if(!name.equals(other.name))false; trueを返します。 }}学生クラスは同等のインターフェイスを実装し、クラス内の比較項目を設定します
import java.util.hashset; import java.util.set;/** *学生クラス * @Author Administrator * */public class Student emblestion CARMANTABLE <Student> {public String id;パブリック文字列名; public set <Course>コース; public Student(string id、string name){this.id = id; this.name = name; this.courses = new Hashset <Course>(); } @override public int hashcode(){final int prime = 31; int result = 1; result = prime * result +((name == null)?0:name.hashcode());返品結果; } @Override public boolean equals(object obj){if(this == obj)return true; if(obj == null)falseを返します。 if(!(obj instanceof student))falseを返します。学生その他=(学生)obj; if(name == null){if(other.name!= null)return false; } else if(!name.equals(other.name))false; trueを返します。 } @override public int compareto(desute o){// IDを比較項目として設定します// todo auto-enerated method stub return this.id.comPareto(o.id); }}コンパレータインターフェイスを実装し、クラス外に比較項目を設定します
Import Java.util.comParator; Public Class StudentComparatorはComparator <Student> {@Override Public Int Compare(Student O1、Student O2){// Todo auto-feenated method stub Returt o1.name.compareto(o2.name); }}学生クラスの比較
/***他のタイプのジェネリックのリストを並べ替え、学生を例に挙げます。 */ public void listcomparatorsort(){list <sudtent> sustentlist = new ArrayList <Student>(); List <Integer> list = getDiffno(4、1000); StudentList.Add(New Student(list.get(0) + ""、 "Mike")); StudentList.Add(New Student(list.get(1) + ""、 "Angela")); StudentList.Add(New Student(list.get(2) + ""、 "Lucy")); StudentList.Add(新しい学生(1000 + ""、 "Beyonce")); System.out.println("----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- System.out.println( "Student:" + Student.id + ":" + sustent.name)// Comparator <t>インターフェイスを実装し、名前の比較コレクションでソートを設定します。 System.out.println("---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------上記はこの記事のすべての内容です。私はそれがすべての人の学習に役立つことを願っています、そして、私は誰もがwulin.comをもっとサポートすることを願っています。