Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按某个参数对对象列表进行排序_Java_Sorting - Fatal编程技术网

Java 按某个参数对对象列表进行排序

Java 按某个参数对对象列表进行排序,java,sorting,Java,Sorting,我有一个包含多个参数的对象和数组列表。 我需要创建一个方法,根据某个参数对列表进行排序。 我的班级是: public class VehicleLoaded { private int vlrefid; private int vehicleRefId; private int load_time; ... public ELPEVehicleLoaded(){} } 我需要按加载时间(升序)对包含此类对象的ArrayList进行排序。 这是我的比较标准: public static Co

我有一个包含多个参数的对象和数组列表。 我需要创建一个方法,根据某个参数对列表进行排序。 我的班级是:

public class VehicleLoaded {

private int vlrefid;
private int vehicleRefId;
private int load_time;
...

public ELPEVehicleLoaded(){}
}
我需要按加载时间(升序)对包含此类对象的ArrayList进行排序。 这是我的比较标准:

public static Comparator<ELPEVehicleLoaded> RouteLoadingTime = new Comparator<ELPEVehicleLoaded>() 
{
    public int compare(ELPEVehicleLoaded vl1, ELPEVehicleLoaded vl2) {

    int vl1_load_time=vl1.getLoadTime();

    int vl2_load_time=vl2.getLoadTime();
    /*For ascending order*/
    return vl1_load_time-vl2_load_time;
}};
publicstaticcomparatorrouteloadingtime=newcomparator()
{
公共整数比较(ELPEVehicleLoaded vl1,ELPEVehicleLoaded vl2){
int vl1_load_time=vl1.getLoadTime();
int vl2_load_time=vl2.getLoadTime();
/*升序*/
返回vl1_加载时间-vl2_加载时间;
}};
因此,我创建了一个静态方法进行排序:

public static void sortListByLoadTime (ArrayList<VehicleLoaded> VLs){
Collections.sort(VLs, new MyComparator());
}
publicstaticvoidsortlistbyloadtime(arraylistvls){
Collections.sort(VLs,新的MyComparator());
}
抱歉,如果这个问题再次被提出,我一直在搜索,但找不到适合我的答案。 提前非常感谢

公共类HelloWorld{
public class HelloWorld {
    public static void main(String[] args) {
        List<MyObj> a = Arrays.asList(new MyObj(5), new MyObj(4),new MyObj(3),new MyObj(2), new MyObj(1));

        Collections.sort(a, Comparator.comparingInt(MyObj::getLoadTime));

        System.out.println(a);
    }
}

class MyObj {
    int loadTime;

    public MyObj(int loadTime) {
        this.loadTime = loadTime;
    }

    public int getLoadTime() {
        return loadTime;
    }

    @Override
    public String toString() {
        return "" + loadTime;
    }
}
公共静态void main(字符串[]args){ 列表a=Arrays.asList(新的MyObj(5)、新的MyObj(4)、新的MyObj(3)、新的MyObj(2)、新的MyObj(1)); Collections.sort(a,Comparator.comparingit(MyObj::getLoadTime)); 系统输出打印项次(a); } } MyObj类{ int加载时间; 公共MyObj(整数加载时间){ this.loadTime=加载时间; } public int getLoadTime(){ 返回装载时间; } @凌驾 公共字符串toString(){ 返回“”+加载时间; } }
其输出将为:

[1,2,3,4,5]

因此,在您的情况下,您需要添加到
sortListByLoadTime
方法中的所有内容是:

Collections.sort(VLs、Comparator.comparingInt(ELPEVehicleLoaded::getLoadTime))

正如我们所讨论的
ArrayList
一样,您应该使用它的排序方法,而不是Collections::sort。在我给出的示例中,它将类似于:

Collections.sort(VLs、Comparator.comparingInt(ELPEVehicleLoaded::getLoadTime))

->
VLs.sort(Comparator.comparingit(elpevehicleloated::getLoadTime))

公共类HelloWorld{
公共静态void main(字符串[]args){
列表a=Arrays.asList(新的MyObj(5)、新的MyObj(4)、新的MyObj(3)、新的MyObj(2)、新的MyObj(1));
Collections.sort(a,Comparator.comparingit(MyObj::getLoadTime));
系统输出打印项次(a);
}
}
MyObj类{
int加载时间;
公共MyObj(整数加载时间){
this.loadTime=加载时间;
}
public int getLoadTime(){
返回装载时间;
}
@凌驾
公共字符串toString(){
返回“”+加载时间;
}
}
其输出将为:

[1,2,3,4,5]

因此,在您的情况下,您需要添加到
sortListByLoadTime
方法中的所有内容是:

Collections.sort(VLs、Comparator.comparingInt(ELPEVehicleLoaded::getLoadTime))

正如我们所讨论的
ArrayList
一样,您应该使用它的排序方法,而不是Collections::sort。在我给出的示例中,它将类似于:

Collections.sort(VLs、Comparator.comparingInt(ELPEVehicleLoaded::getLoadTime))


->
VLs.sort(Comparator.comparingit(elpevehicleloated::getLoadTime))

您可以在函数
中使用
比较器
,如下所示:

VLs.sort((o1,o2) -> o1.getLoadTime() - o2.getLoadTime());
Collections.sort(VLs, ( o1,  o2) -> {
      return o1.getLoadTime() - o2.getLoadTime();
});
Collections.sort(VLs, Comparator.comparing(VehicleLoaded::getLoadTime));
或者您可以使用
Collections实用程序
,如下所示:

VLs.sort((o1,o2) -> o1.getLoadTime() - o2.getLoadTime());
Collections.sort(VLs, ( o1,  o2) -> {
      return o1.getLoadTime() - o2.getLoadTime();
});
Collections.sort(VLs, Comparator.comparing(VehicleLoaded::getLoadTime));
lambda表达式
可替换为
方法参考
,如下所示:

VLs.sort((o1,o2) -> o1.getLoadTime() - o2.getLoadTime());
Collections.sort(VLs, ( o1,  o2) -> {
      return o1.getLoadTime() - o2.getLoadTime();
});
Collections.sort(VLs, Comparator.comparing(VehicleLoaded::getLoadTime));

您可以在函数
sortListByLoadTime
中使用
comparator
,如下所示:

VLs.sort((o1,o2) -> o1.getLoadTime() - o2.getLoadTime());
Collections.sort(VLs, ( o1,  o2) -> {
      return o1.getLoadTime() - o2.getLoadTime();
});
Collections.sort(VLs, Comparator.comparing(VehicleLoaded::getLoadTime));
或者您可以使用
Collections实用程序
,如下所示:

VLs.sort((o1,o2) -> o1.getLoadTime() - o2.getLoadTime());
Collections.sort(VLs, ( o1,  o2) -> {
      return o1.getLoadTime() - o2.getLoadTime();
});
Collections.sort(VLs, Comparator.comparing(VehicleLoaded::getLoadTime));
lambda表达式
可替换为
方法参考
,如下所示:

VLs.sort((o1,o2) -> o1.getLoadTime() - o2.getLoadTime());
Collections.sort(VLs, ( o1,  o2) -> {
      return o1.getLoadTime() - o2.getLoadTime();
});
Collections.sort(VLs, Comparator.comparing(VehicleLoaded::getLoadTime));

您可以尝试Java 8方式:

VLs.stream()
    .sorted((v1,v2)-> {
        if(v1.getLoadTime()<v2.getLoadTime()) {
            return -1;
        }
        if(v1.getLoadTime()>v2.getLoadTime()) {
            return 1;
        }
        return 0;
    }).collect(Collectors.toList());
VLs.stream()
.已排序((v1,v2)->{
if(v1.getLoadTime()v2.getLoadTime()){
返回1;
}
返回0;
}).collect(Collectors.toList());

您可以尝试Java 8方式:

VLs.stream()
    .sorted((v1,v2)-> {
        if(v1.getLoadTime()<v2.getLoadTime()) {
            return -1;
        }
        if(v1.getLoadTime()>v2.getLoadTime()) {
            return 1;
        }
        return 0;
    }).collect(Collectors.toList());
VLs.stream()
.已排序((v1,v2)->{
if(v1.getLoadTime()v2.getLoadTime()){
返回1;
}
返回0;
}).collect(Collectors.toList());

有什么问题吗?您不应该传入RouteLoadingTime而不是新的MyComparator()?
Collections.sort(a,Comparator.comparating(ELPEVehicleLoaded::getLoadTime))看起来你的想法是对的。那么困难是什么呢?等等,如果我有Collections.sort(VLs,vl.RouteLoadingTime);vl作为访问车辆加载的对象是否有效?问题是什么?是否应该传入RouteLoadingTime而不是新的MyComperator()?
Collections.sort(a,Comparator.comparating(ELPEVehicleLoaded::getLoadTime))看起来你的想法是对的。那么困难是什么呢?等等,如果我有Collections.sort(VLs,vl.RouteLoadingTime);如果vl是一个访问车辆加载的对象,那么它会工作吗?或者因为Java8只是
a.sort(Comparator.comparingInt(MyObj::getLoadTime))我做了。。。但是我遗漏了什么呢?或者因为Java8只是
a.sort(Comparator.comparingit(MyObj::getLoadTime))我做了。。。但我错过了什么?这很好,也可以理解@DimitrisBoukosis你知道这段代码不会对VLs中的元素进行排序,对吗?因此,您必须执行
VLs=VLs.stream().sorted(lambda).collect(new ArrayList(…)
,或者必须直接将ArrayList::sort引用为:
VLs.sort(lambda)
,而不是ArrayList::stream。此外,引用的lambda相当于Comparator::comparingit,任何现代IDE都会突出其主体,并建议使用通用的主体。这是一个被误用的@Yug Singh答案版本(可能会为免费RPs做一些修改)…这很好,也可以理解@DimitrisBoukosis你知道这段代码不会对VLs中的元素进行排序,对吗?因此,您必须要么执行