英文:
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<MyPojo>. 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<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());
    }
}
答案2
得分: 2
你可以使用TreeMap和Collectors.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<Integer, List<MyPojo>> map = pojos.stream()
                                          .collect(Collectors.groupingBy(
                                              MyPojo::getPriority, 
                                              TreeMap::new, 
                                              Collectors.toList()
                                           ));
List<MyPojo> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论