Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/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 8 使用Streams根据条件筛选最大值列表_Java 8 - Fatal编程技术网

Java 8 使用Streams根据条件筛选最大值列表

Java 8 使用Streams根据条件筛选最大值列表,java-8,Java 8,我有一个自定义对象列表,如下所示,我需要根据客户id和betOfferId筛选出最大赌注值,但客户id不应重复(重复),基本上对于特定betOfferId,我希望获得一个客户最高赌注值列表,并将其限制为20个客户,对于特定的赌注最大 .values() .stream() .collect(groupingBy(T::getStake)) .values() .stream() .flatMap(x -> x.stream().limit(20)) .collect(Collectors.

我有一个自定义对象列表,如下所示,我需要根据客户id和betOfferId筛选出最大赌注值,但客户id不应重复(重复),基本上对于特定betOfferId,我希望获得一个客户最高赌注值列表,并将其限制为20个客户,对于特定的赌注最大

.values()
.stream()
.collect(groupingBy(T::getStake))
.values()
.stream()
.flatMap(x -> x.stream().limit(20))
.collect(Collectors.toList());
例如,对于下面的列表,如果我给betofferid as 999,我需要得到这样的结果,因为有2个客户,这很好。 { “客户ID”:12345, “betOfferId”:999, “赌注”:9000 }及{ “客户ID”:1234, “betOfferId”:999, “赌注”:8500 }

考虑到赌注 { “客户ID”:1234, “贝托弗里德”:888, “赌注”:4500 }


]

我将这样做:

List<T> result = source.stream()
            .filter(x -> x.getOfferId() == offerId)
            .collect(toMap(T::getCustomerId,
                    Function.identity(),
                    BinaryOperator.maxBy(Comparator.comparingInt(T::getStake))))
            .values()
            .stream()
            .collect(groupingBy(T::getStake))
            .values()
            .stream()
            .flatMap(x -> x.stream().limit(20))
            .collect(Collectors.toList());
现在,我们限制20个客户最多为一个特定的股份

.values()
.stream()
.collect(groupingBy(T::getStake))
.values()
.stream()
.flatMap(x -> x.stream().limit(20))
.collect(Collectors.toList());
其中
T
是包含
客户ID
桩号
等的类

.values()
.stream()
.collect(groupingBy(T::getStake))
.values()
.stream()
.flatMap(x -> x.stream().limit(20))
.collect(Collectors.toList());