Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/389.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/8/sorting/2.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_Java 8_Maxby - Fatal编程技术网

Java 如何根据列表中的属性对对象列表进行排序

Java 如何根据列表中的属性对对象列表进行排序,java,sorting,java-8,maxby,Java,Sorting,Java 8,Maxby,我有一个对象列表,比如Notification,它有三个列表-区域范围、传感器范围和区域范围。我需要结合所有3个列表,根据最大百分比对通知对象列表进行排序 以下是我的通知对象: { "notificationId": 26, "areaWise": [], "sensorWise": [ {

我有一个对象列表,比如Notification,它有三个列表-
区域范围
传感器范围
区域范围
。我需要结合所有3个列表,根据最大百分比对通知对象列表进行排序

以下是我的通知对象:

{
                "notificationId": 26,
                "areaWise": [],
                "sensorWise": [
                    {
                        "id": 3,
                        "threshold": 1,
                        "headCount": 113,
                        "pecentage": 11200
                    },
                    {
                        "id": 4,
                        "threshold": 1,
                        "headCount": 108,
                        "pecentage": 10700
                    },
                    {
                        "id": 5,
                        "threshold": 1,
                        "headCount": 108,
                        "pecentage": 10700
                    },
                    {
                        "id": 7,
                        "threshold": 1,
                        "headCount": 91,
                        "pecentage": 9000
                    }
                ],
                "regionWise": []
            },
@数据
公共类项目{
私有int通知ID;
按区域划分的私有列表;
私人名单;
按区域列出私人名单;
@资料
公共静态类{
私有int-id;
私有整数阈值;
私人机构编制;
私人专业;
}
}
课堂测试{
静态void main(){
ArrayList项=新的ArrayList(10);
项目。排序(比较器。比较)(
项目->
Stream.of(item.getAreaWise()、item.getSensorWise()、item.getRegionWise())
.flatMapToInt(wises->wises.stream().mapToInt(Item.Wise::GetSpecentage))
.max().orElse(整数.MIN_值)
));
}
}
@数据
公共类项目{
私有int通知ID;
按区域划分的私有列表;
私人名单;
按区域列出私人名单;
@资料
公共静态类{
私有int-id;
私有整数阈值;
私人机构编制;
私人专业;
}
}
课堂测试{
静态void main(){
ArrayList项=新的ArrayList(10);
项目。排序(比较器。比较)(
项目->
Stream.of(item.getAreaWise()、item.getSensorWise()、item.getRegionWise())
.flatMapToInt(wises->wises.stream().mapToInt(Item.Wise::GetSpecentage))
.max().orElse(整数.MIN_值)
));
}
}

到目前为止您尝试了什么?我尝试了使用:tpaList.forEach(data->{data.getNotificationDataSet().stream().map(item->{stream.of(item.getAreaWise(),item.getSensorWise(),item.getRegionWise()).sorted(Comparator.comparing(AggregateData::getPecentage));return item;}.collect(collector.toList());但要对每个列表进行排序,我需要对notificationDataSet进行排序。在对列表进行排序时,您通常会实现Compariable接口,然后使用collections排序。compare方法将具有一个自定义逻辑,该逻辑获取对象百分比,然后将其与返回的其他对象的百分比(较大)进行比较。您尝试过吗?是吗这里有一个它不起作用的原因吗?将所有3个列表合并是什么意思?当百分比仅在sensorwise中定义时,如何在计算中使用areawise和regionwise数组?到目前为止您尝试了什么?我尝试了使用:tpaList.forEach(data->{data.getNotificationDataSet().stream().map(item->{stream.of(item.getAreaWise()),item.getSensorWise(),item.getRegionWise()).sorted(Comparator.comparing(AggregateData::getPecentage));return item;}).collect(Collectors.toList());但要对每个列表进行排序,我需要对notificationDataSet进行排序。在对列表进行排序时,您通常会实现Compariable接口,然后使用collections排序。compare方法将具有一个自定义逻辑,该逻辑获取对象百分比,然后将其与返回的其他对象的百分比(较大)进行比较。您尝试过吗?是吗这里有一个不起作用的原因吗?你将所有3个列表合并是什么意思?当百分比仅在sensorwise中定义时,你如何在计算中使用areawise和regionwise数组?你能告诉我我是否有公共类ThresholdPercentageAlert这样的结构吗{Integer alertTypeId;//List notificationDataSet;List notificationDataSet;我想根据相同的条件对地图列表进行排序。我不明白您的问题,可能是另一个问题,如果您发布了一个新问题,请您告诉我是否有类似于公共类ThresholdPercentageAlert的结构,好吗{Integer alertTypeId;//List notificationDataSet;List notificationDataSet;我想根据相同的条件对地图列表进行排序。我不明白您的问题,可能是另一个问题,您是否发布了新问题?
@Data
public class Item {

    private int notificationId;
    private List<Wise> areaWise;
    private List<Wise> sensorWise;
    private List<Wise> regionWise;

    @Data
    public static class Wise {
        private int id;
        private int threshold;
        private int headCount;
        private int pecentage;
    }
}

class Test {
    static void main() {
        ArrayList<Item> items = new ArrayList<>(10);

        items.sort(Comparator.comparingInt(
                item ->
                    Stream.of(item.getAreaWise(), item.getSensorWise(), item.getRegionWise())
                            .flatMapToInt(wises -> wises.stream().mapToInt(Item.Wise::getPecentage))
                .max().orElse(Integer.MIN_VALUE)
        ));
    }
}