在java中对对象数组进行排序的最快方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29777696/
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
Fastest way to sort an array of objects in java
提问by user1010101
I have a Class called apple which contains 3 values as int x
, int y
and int weight
. Then i created an array of apple type objects. Now i want to sort the the array of objects based on weight meaning the the apple object with the lowest weight should be first and so on.
我有一个名为 apple 的类,其中包含 3 个值,如int x
,int y
和int weight
. 然后我创建了一个苹果类型对象的数组。现在我想根据重量对对象数组进行排序,这意味着重量最低的苹果对象应该排在第一位,依此类推。
I know there are quite a few ways to achieve this by using Arrays.sort etc or comparators.
我知道有很多方法可以通过使用 Arrays.sort 等或比较器来实现这一点。
I was wondering what is the fastest way of doing this sort in Java? There can be a case where i have 500,000 objects so i want to know which sort i should use, more importantly which approach will give me best approach. i have even wrote my own quick sort with Hoare partition.
我想知道在 Java 中进行这种排序的最快方法是什么?可能有一种情况,我有 500,000 个对象,所以我想知道我应该使用哪种类型,更重要的是哪种方法会给我最好的方法。我什至用 Hoare 分区编写了自己的快速排序。
Code for Apple class
Apple 类的代码
public class Apple {
public int x;
public int y;
public int weight;
public Apple(int a, int b, int w) {
x = a;
y = b;
weight = w;
}
}
Code for main class
主类代码
public class main {
static Apple[] appleArray;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
int totalApples = sc.nextInt();
appleArray = new Edge[totalApples];
int x = 10;
int y = 20;
int w = 30;
for (int i = 0; i < size; i++) {
appleArray[i] = new Apple(x, y, w);
x++;
y++;
w++;
}
//Now i want to sort array of apple objects based on weight
}
}
采纳答案by seanhodges
This book has a useful cheat sheet for deciding the optimal sort for your needs: https://www.safaribooksonline.com/library/view/algorithms-in-a/9780596516246/ch04s09.html
这本书有一个有用的备忘单,可以根据您的需要确定最佳排序:https: //www.safaribooksonline.com/library/view/algorithms-in-a/9780596516246/ch04s09.html
The easiest solution
最简单的解决方案
The Arrays.sort
command uses a quicksort implementation, which is suitable for many situations. For your example code this might be:
该Arrays.sort
命令使用快速排序实现,适用于多种情况。对于您的示例代码,这可能是:
Arrays.sort(appleArray, new Comparator<Apple>(){
@Override
public int compare(Apple apple1, Apple apple2){
return apple1.weight - apple2.weight;
}
});
The fastest solution
最快的解决方案
In your case you have a large array containing repetitions, for example 50,000 apples in your array might all weigh 3 ounces... You might therefore opt to implement a bucket sortto improve performance over a quicksort, which can be wasteful under such conditions. Here is an example implementation.
在您的情况下,您有一个包含重复的大型数组,例如,您的数组中的 50,000 个苹果可能都重 3 盎司……因此,您可能会选择实施桶排序来提高快速排序的性能,这在这种情况下可能会很浪费。这是一个示例实现。
Perhaps benchmark a few researched choices, using the Java API when it suits, to determine the best solution for your input set.
也许对一些研究过的选择进行基准测试,在合适的时候使用 Java API,以确定输入集的最佳解决方案。
回答by Davide Lorenzo MARINO
You can use Arrays.sort
passing a custom Comparator
or defining your Apple
as Comparable
您可以使用Arrays.sort
传递自定义Comparator
或定义Apple
为Comparable
回答by m0skit0
回答by Ashu Phaugat
We can use Collections.sort with a custom Comparator.
我们可以将 Collections.sort 与自定义比较器一起使用。
class Apple {
public final int weight;
// ...
};
List<Apple> apples = // ...
Collections.sort(apples, new Comparator<Apple>() {
@Override public int compare(Apple a1, Apple a2) {
return a1.weight - a2.weight; // Ascending
}
});
回答by Gaurav Jeswani
If in your object there is any Number or Integer over which you have to sort you can do like this-
如果在您的对象中有任何数字或整数,您必须对其进行排序,您可以这样做 -
List<Object> obj = new ArrayList<Object>();
Collections.sort(obj, new Comparator<Object>() {
@Override
public int compare(Object object1, Object object2) {
return object1.getNumber() > object2.getNumber() ? 1 : -1;
}
});
And if there is not Number or Integer over which you can sort it and you are just having Strings in your object than assign value to Strings by enum.
如果没有数字或整数,您可以对其进行排序,并且您的对象中只有字符串,而不是通过枚举为字符串赋值。
enum Code {
Str1(1), Str2(2), Str3(3), Str4(4), Str5(5));
int sortNumber;
Code(int sortNumber) {
this.sortNumber = sortNumber;
}
int returnNumber() {
return sortNumber;
}
};
public static void main(String[] args) {
List<Object> obj = new ArrayList<Object>();
Collections.sort(obj, new Comparator<Object>() {
@Override
public int compare(Object object1, Object object2) {
return Code.valueOf(object1.getStr()).returnNumber() > Code.valueOf(object2.getStr()).returnNumber() ? 1 : -1;
}
});
}