コンパレータはインターフェイスであり、価格比較関数の2つの比較()と等しい()の2つの方法をオーバーライドできます。 nullの場合、a、b、c、d、e、f、g、つまりa、b、c、d、e、f、gなどの要素のデフォルトの順序を使用します。
比較(a、b)方法:最初のパラメーターに従って負の整数、ゼロまたは正の整数を返します。
Equals(OBJ)メソッド:指定されたオブジェクトがコンパレータである場合にのみTRUEを返し、このコンパレータと同じ種類が実装されることを余儀なくされます。
collections.sort(list、new PriceComparator())の2番目のパラメーター;フラグに相当するINTタイプの値を返し、リストをソートする順序でソートメソッドを伝えます。
特定の実装コードメソッドは次のとおりです。
ブックエンティティクラス:
パッケージcom.tjcyjd.comparator; java.text.decimalformatをインポートします。 java.text.simpledateformatをインポートします。 java.util.gregoriancalendarをインポートします。 java.util.iteratorをインポートします。 java.util.treemapをインポートします。 / ** * book entity class * * @author yjd * * */ public class book実装{//オブジェクトクラスpublic int id; // number public string name; // name public double priceからデフォルトで継承される本のクラスを定義する本を定義します。 //プライベートストリング著者; //著者パブリックグレゴリアンカレンダーカレンダー; //出版日パブリックブック(){this(0、 "x"、0.0、new Gregoriancalendar()、 ""); } public Book(int id、string name、double price、gregoriancalendarカレンダー、文字列著者){this.id = id; this.name = name; this.price = price; this.calendar = calendar; this.author =著者; } // bookクラスの要件を満たすために親クラスオブジェクトから継承されたメソッドの書き換えpublic string toString(){string showstr = id + "/t" + name; //クラス情報decimalformat formatpriceを表示する文字列を定義します= new Decimalformat( "0.00"); // 2つの小数showstr + = "/t" + formatprice.format(formatprice.format(// format showtr + = "/t" +著者; SimpleDateFormat formatdate = new SimpledateFormat( "yyyy year mm month dd day"); showtr + = "/t" + formatdate.format(calendar.getTime()); //フォーマット時間return shostr; // class情報stringを返す} public int compareto(object obj){//比較可能なインターフェイスのメソッドbook b =(book)obj; this.id -b.idを返します。 //デフォルトのソートに使用される本のIDに従ってサイズを比較します} public static void main(string [] args){book b1 = new Book(10000、 "Dream of the Red Chamber"、150.86、New Gregoriancalendar(2009、01、25)、 "cao xueqin、gao e"); Book B2 = New Book(10001、「3つの王国のロマンス」、99.68、新しいグレゴリアンカレンダー(2008、7、8)、「Luo Guanzhong」); Book B3 = new Book(10002、 "Water Margin"、100.8、New Gregoriancalendar(2009、6、28)、 "Shi nai'an"); Book B4 = New Book(10003、「Journey To the West」、120.8、New GregorianCalendar(2011、6、8)、「Wu Cheng'en」); Book B5 = New Book(10004、「The Legend of the Condor Heroes」、10.4、New GregorianCalendar(2011、9、23)、「Sohu」); treemap tm = new Treemap(); tm.put(b1、new Integer(255)); tm.put(b2、new Integer(122)); tm.put(b3、new Integer(688)); tm.put(b4、new Integer(453)); tm.put(b5、new Integer(40)); iterator it = tm.keyset()。iterator();オブジェクトキー= null、value = null; book bb = null; while(it.hasnext()){key = it.next(); bb =(book)key; value = tM.get(key); system.out.println(bb.tostring() + "/t inventory:" + tmeg(key)); }}}カスタムコンパレータとテストクラス:
パッケージcom.tjcyjd.comparator; java.util.arraylistをインポートします。 java.util.collectionsをインポートします。 java.util.comparatorをインポートします。 java.util.gregoriancalendarをインポートします。 java.util.iteratorをインポートします。 java.util.listをインポートします。 public class usecomparator {public static void main(string args []){list <book> list = new arraylist <book>(); //配列シーケンスブックB1 =新しい本(10000、「Dream of Red Mansions」、150.86、New GregorianCalendar(2009、01、25)、「Cao Xueqin、Gao E」); Book B2 = New Book(10001、「3つの王国のロマンス」、99.68、新しいグレゴリアンカレンダー(2008、7、8)、「Luo Guanzhong」); Book B3 = new Book(10002、 "Water Margin"、100.8、New Gregoriancalendar(2009、6、28)、 "Shi nai'an"); Book B4 = New Book(10003、「Journey To the West」、120.8、New GregorianCalendar(2011、6、8)、「Wu Chengen」); Book B5 = New Book(10004、 "Tianlong Baba"、10.4、New GregorianCalendar(2011、9、23)、 "Sohu"); list.add(b1); list.add(b2); list.add(b3); list.add(b4); list.add(b5); // collections.sort(list); //デフォルトのコンパレータ、System.out.println( "配列シーケンスの要素:")はありません。 myprint(list); collections.sort(list、new PriceComparator()); //価格System.out.println( "book価格でソート:")で並べ替えます。 myprint(list); collections.sort(list、new CalendarComparator()); // time system.out.println( "book by book by public static void myprint(list <book> list){iterator it = list.iterator(); //リスト内のすべての要素をトラバースするためのイテレーターを取得します。 it.next()); double(p2.price);インターフェイスブックp1 =(book)object1; // casting book p2 =(book)object2; p2.calendar.compareto(p1.calendar)を返します。 }}}上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。