本文目錄一覽:
- 1、java里的Collections類中的靜態方法sort()是怎麼用比較器比較兩個對象?
- 2、java中是不是有個方法能直接返回一個比較器的
- 3、java中Comparable和Comparator兩種比較器的區別
- 4、JAVA。比較器 JAVA比較器裏面那個compare函數怎麼實現升序或降序排列啊,retur
- 5、比較comparable和Comparator
java里的Collections類中的靜態方法sort()是怎麼用比較器比較兩個對象?
compareto的確是返回一個(-1,0,1)的值。
pricecomparator
類的compare方法調用compareto,他的返回值也是(-1,0,1)中的一個。
collections.sort方法就是按照pricecomparator
類的compare方法來比較list的各個元素,進行排序。
collections.sort的源碼我沒有看過,不知道他具體使用什麼方法排序,不過排序算法就那麼幾類:選擇排序、插入排序、交換排序。估計是快速排序吧。
java中是不是有個方法能直接返回一個比較器的
java中每個方法的返回值,有且僅有一個,因為方法中一旦使用return語句返回返回值,整個方法就會結束,下面的語句也將不會執行。
java中如果想返回多個值的話,可以採用數組和集合進行存儲,然後返回集合或者數組即可。其中數組用來封裝基本數據類型的數據,集合用來封裝對象數據。可以根據返回值的實際類型來選擇容器。
java中Comparable和Comparator兩種比較器的區別
Comparable和Comparator接口都是為了對類進行比較,眾所周知,諸如Integer,double等基本數據類型,java可以對他們進行比較,而對於類的比較,需要人工定義比較用到的字段比較邏輯。可以把Comparable理解為內部比較器,而Comparator是外部比較器,基本的寫法如下:
class Apple implements ComparableApple{
int id;
double price;
public Apple(int id, double price) {
this.id = id;
this.price = price;
}
public int compareTo(Apple o) {
//return Double.compare(this.getPrice(),o.getPrice());
if (Math.abs(this.price-o.price)0.001)
return 0;
else
return (o.price-this.price)0?1:-1;
}
@Override
public String toString() {
return “Apple{” +
“id=” + id +
“, price=” + price +
‘}’;
}
}
class AESComparator implements ComparatorApple{
public int compare(Apple o1, Apple o2) {
if (Math.abs(o1.price-o2.price)0.001)
return 0;
else{
return (o1.price-o2.price)0?1:-1;
}
}
}
實現了Comparable接口的類需要實現compareTo()方法,傳入一個外部參數進行比對,實現了Comparator接口的方法需要實現compare()方法,對外部傳入的兩個類進行比較,從而讓外部方法在比較時調用。
兩者的區別是實現Comparator接口代碼更加靈活,可以定義某個類的多個比較器,從而在排序時根據實際場景自由調用,而Comparable接口實現後便不能改動。兩種接口的調用方式如下:
class AESComparator implements ComparatorApple{
public int compare(Apple o1, Apple o2) {
if (Math.abs(o1.price-o2.price)0.001)
return 0;
else{
return (o1.price-o2.price)0?1:-1;
}
}
}
class DESComparator implements ComparatorApple{
public int compare(Apple o1, Apple o2) {
if (Math.abs(o1.price-o2.price)0.001)
return 0;
else {
return (o1.price-o2.price)0?-1:1;
}
}
}
public static void main(String[] args) {
Apple apple1 = new Apple(1,4.8);
Apple apple2 = new Apple(2,5.9);
Apple apple3 = new Apple(3,8.5);
ListApple list = new ArrayListApple();
list.add(apple1);
list.add(apple3);
list.add(apple2);
System.out.println(“Comparable==========”);
System.out.printf(“this list of apples: %s\n”,list);
Collections.sort(list);
System.out.printf(“this list of apples: %s\n”,list);
System.out.println(“Comparator==========”);
System.out.printf(“this list of apples: %s\n”,list);
Collections.sort(list,new DESComparator());
System.out.printf(“this list of apples: %s\n”,list);
Collections.sort(list,new AESComparator());
System.out.printf(“this list of apples: %s\n”,list);
}
}
上述代碼存在的問題,不能在比較器中進行double類型的減法操作,因為對於值比較大的double,減法操作容易導致值的溢出,java7對每一種包裝類型的比較新增了compare()方法,改造後的代碼如下:
class Apple implements ComparableApple{
int id;
double price;
public Apple(int id, double price) {
this.id = id;
this.price = price;
}
public int compareTo(Apple o) {
return Double.compare(this.price,o.price);
}
@Override
public String toString() {
return “Apple{” +
“id=” + id +
“, price=” + price +
‘}’;
}
}
class AESComparator implements ComparatorApple{
public int compare(Apple o1, Apple o2) {
return Double.compare(o1.price,o2.price);
}
}
class DESComparator implements ComparatorApple{
public int compare(Apple o1, Apple o2) {
return Double.compare(o2.price,o1.price);
}
}
查看Double.compare的源碼如下
public static int compare(double d1, double d2) {
if (d1 d2)
return -1; // Neither val is NaN, thisVal is smaller
if (d1 d2)
return 1; // Neither val is NaN, thisVal is larger
// Cannot use doubleToRawLongBits because of possibility of NaNs.
long thisBits = Double.doubleToLongBits(d1);
long anotherBits = Double.doubleToLongBits(d2);
return (thisBits == anotherBits ? 0 : // Values are equal
(thisBits anotherBits ? -1 : // (-0.0, 0.0) or (!NaN, NaN)
1)); // (0.0, -0.0) or (NaN, !NaN)
}
JAVA。比較器 JAVA比較器裏面那個compare函數怎麼實現升序或降序排列啊,retur
想當於一個比較規則,每兩個一比較,如果返回正數則前者大,返回負數就後者大,直接return a-b就能實現,不需要用if else了
比較comparable和Comparator
Comparable Comparator 都是用來實現集合中的排序的 只是 Comparable 是在集合內部定義的方法實現的排序 Comparator 是在集合外部實現的排序 所以 如想實現排序 就需要在集合外定義 Comparator 接口的方法或在集合內實現 Comparable 接口的方法 具體請看 Thinking in java Comparable 是一個對象本身就已經支持自比較所需要實現的接口(如 String Integer 自己就可以完成比較大小操作) 而 Comparator 是一個專用的比較器 當這個對象不支持自比較或者自比較函數不能滿足你的要求時 你可以寫一個比較器來完成兩個對象之間大小的比較 可以說一個是自已完成比較 一個是外部程序實現比較的差別而已 用 Comparator 是策略模式(strategy design pattern) 就是不改變對象自身 而用一個策略對象(strategy object)來改變它的行為 比如 你想對整數採用絕對值大小來排序 Integer 是不符合要求的 你不需要去修改 Integer 類(實際上你也不能這麼做)去改變它的排序行為 只要使用一個實現了 Comparator 接口的對象來實現控制它的排序就行了 java 代碼 // AbsComparator java import java util *;public class AbsComparator implements Comparator { public int pare(Object o Object o ) { int v = Math abs(((Integer)o ) intValue()); int v = Math abs(((Integer)o ) intValue()); return v v ? : (v == v ? : ); } }可以用下面這個類測試 AbsComparator
// Test javaimport java util *;
public class Test { public static void main(String[] args) {
//產生一個 個隨機整數的數組(有正有負) Random rnd = new Random(); Integer[] integers = new Integer[ ]; for(int i = ; i integers length; i++) integers[i] = new Integer(rnd nextInt( ) * (rnd nextBoolean() ? : ));
system out println( 用Integer內置方法排序 ); Arrays sort(integers); system out println(Arrays asList(integers));
lishixinzhi/Article/program/Java/hx/201311/25976
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/275853.html