Java 这个比较器是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23136998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does this comparator work?
提问by
package vehicles_order;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
public class vehicles implements Comparable {
String Vehicle;
String Make;
String Type;
double Cost;
public static void main(String[] args){
ArrayList cars = new ArrayList();
cars.add(new vehicles ("CAR","Peugot","3008",12500.00));
cars.add(new vehicles ("CAR","BMW","316",4995.00));
cars.add(new vehicles ("CAR","Ford","Fiesta",2995.00));
Collections.sort(cars);
Iterator itr = cars.iterator();
while(itr.hasNext()){
Object element = itr.next();
System.out.println(element + "\n");
}
}
public vehicles(String vehicle, String make, String type, double cost){
Vehicle = vehicle;
Make = make;
Type = type;
Cost = cost;
}
public String toString() {
return "Vehicle: " + Vehicle + "\n" + "Make: " + Make + "\n" + "Type: " + Type + "\n" + "Cost: " + Cost;
}
public int compareTo(Object o1) {
if (this.Cost == ((vehicles) o1).Cost)
return 0;
else if ((this.Cost) > ((vehicles) o1).Cost)
return 1;
else
return -1;
}
}
My comparator is at the bottom and I just wondering how it actually works. Im guessing its like a stack, where when it returns 1 it moves up the stack if its -1 moves down.
我的比较器在底部,我只是想知道它实际上是如何工作的。我猜它就像一个堆栈,当它返回 1 时,如果它的 -1 向下移动,它就会向上移动堆栈。
Also, can anyone tell me how id go about ordering the cars via a different method. For example, store the cost in a temp highest value and check if the new value is higher than the current one
另外,谁能告诉我如何通过不同的方法订购汽车。例如,将成本存储在临时最高值中并检查新值是否高于当前值
采纳答案by Denis Kulagin
What is a Comparable interface? It is an interface containing single method:
什么是 Comparable 接口?它是一个包含单个方法的接口:
compareTo(T o)
providing capability for Java to comprare your object with any other. Basically you would like to compare your object with those of the same kind (class). So there is a usual check in compareToimplementation:
为 Java 提供将您的对象与任何其他对象进行比较的能力。基本上,您想将您的对象与同类(类)的对象进行比较。所以在compareTo实现中有一个通常的检查:
if (o instanceof vehicles) {
Vehicles v = (Vehicles)o;
// compare
} else {
return false;
}
Then, in compare section you should decide, based on your business-rules, whether other vehicle is equals, more of less to your object.
然后,在比较部分,您应该根据您的业务规则决定其他车辆是否与您的对象相同,或更多或更少。
- 0 - if they are equal;
- 1 - if your object is greater;
- -1 - if your object is lesser.
- 0 - 如果它们相等;
- 1 - 如果您的对象更大;
- -1 - 如果您的对象较小。
All that simple!
就这么简单!
回答by Anubian Noob
回答by Amit P
Collections.sort(List list) uses this comapreTo() method to decide natural ordering of your objects. see http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html
Collections.sort(List list) 使用这个 comapreTo() 方法来决定对象的自然顺序。见http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html
回答by Yirga
It uses bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
它使用冒泡排序,有时也称为下沉排序,是一种简单的排序算法,它重复遍历要排序的列表,比较每对相邻的项目,如果它们的顺序错误,则交换它们。重复遍历列表直到不需要交换,这表明列表已排序。