如何使用Java流获取具有最高值的所有元素?

huangapple go评论77阅读模式
英文:

How to get all elements with the highest value with Java streams?

问题

我有以下的POJO

class MyPojo {
String name;
int priority;
}


我有一个 `List<MyPojo>`。现在,我想要检索所有具有最高优先级的元素。这些元素的顺序并不重要。

在Java流中是否有可能实现这个?我认为我应该先按优先级分组,然后获取属于最高优先级的所有元素,但是我不太确定如何以高效的方式做到这一点。
英文:

I have the following pojo:

class MyPojo {
    String name;
    int priority;
}

I have a List&lt;MyPojo&gt;. Now, I want to retrieve all elements which have the highest priority. The order of those elements does not matter.

Is this possible with Java streams? I think I should first group by priority and then get all elements belonging to the highest priority, but I am not sure how to do this in an efficient manner.

答案1

得分: 5

你应该将其转换为 IntStream 并从中获取 max()
然后获取具有此值的每个 pojo。

import java.util.List;
import java.util.stream.Collectors;

class MyPojo {
    String name;
    int priority;

    public int getPriority() {
        return priority;
    }
}

public class Main {
    public static void main(String[] args) {
        List<MyPojo> list = null;
        int max = list.stream().mapToInt(MyPojo::getPriority).max().orElse(Integer.MIN_VALUE);
        List<MyPojo> maxPojos = list.stream().filter(pojo -> pojo.getPriority() == max).collect(Collectors.toList());
    }
}
英文:

You should cast it to IntStream and get the max() out of it.
And then get every pojos with this value.

import java.util.List;
import java.util.stream.Collectors;

class MyPojo {
    String name;
    int priority;

    public int getPriority() {
        return priority;
    }
}

public class Main {
    public static void main(String[] args) {
        List&lt;MyPojo&gt; list = null;
        int max = list.stream().mapToInt(MyPojo::getPriority).max().orElse(Integer.MIN_VALUE);
        List&lt;MyPojo&gt; maxPojos = list.stream().filter(pojo -&gt; pojo.getPriority() == max).collect(Collectors.toList());
    }
}

答案2

得分: 2

你可以使用TreeMapCollectors.groupingBy()来实现这个功能:

TreeMap<Integer, List<MyPojo>> map = pojos.stream()
                                          .collect(Collectors.groupingBy(
                                              MyPojo::getPriority, 
                                              TreeMap::new, 
                                              Collectors.toList()
                                           ));

List<MyPojo> maxPrios = map.lastEntry().getValue();

lastEntry()将返回具有最高优先级的MyPojo,因为Integer的自然排序中,最小的值会排在前面,最大的值会排在后面。

英文:

You can do this using a TreeMap and the Collectors.groupingBy():

TreeMap&lt;Integer, List&lt;MyPojo&gt;&gt; map = pojos.stream()
                                          .collect(Collectors.groupingBy(
                                              MyPojo::getPriority, 
                                              TreeMap::new, 
                                              Collectors.toList()
                                           ));

List&lt;MyPojo&gt; maxPrios = map.lastEntry().getValue();

The lastEntry() will return the pojos with the highest priority, due to the natural ordering of Integers where the smallest value will be first, and the largest value will be last.

huangapple
  • 本文由 发表于 2020年10月12日 15:13:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64313188.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定