简体   繁体   English

.collect(Collectors.toList())和Java方法上的Streams

[英].collect(Collectors.toList()) and Streams on Java Method

I have a collection (as hashmap) of Doctors, into a generic Hospital class. 我有一个医生的集合(作为hashmap),进入一个通用的医院类。

Map<Integer, Doctor> doctors = new HashMap<Integer, Doctor>();

For each doctor I have some information such as in the class code (focus on the patients): 对于每位医生,我都有一些信息,例如课堂代码(专注于患者):

public class Doctor extends Person {
    private int id;
    private String specialization;
    private List<Person> patients = new LinkedList<Person>();

My purpose is to write this function which return busy doctors: doctors that has a number of patients larger than the average. 我的目的是编写这个功能,让繁忙的医生回归:医生的病人数量大于平均水平。

/**
 * returns the collection of doctors that has a number of patients larger than the average.
 */
Collection<Doctor> busyDoctors(){

    Collection<Doctor> doctorsWithManyPatients = 
            doctors.values().stream()
            .map( doctor -> doctor.getPatients() )
            .filter( patientsList -> { return patientsList.size() >= AvgPatientsPerDoctor; })
            .collect(Collectors.toList());

    return null;
}

I want to use the streams as above to perform this operation. 我想使用上面的流来执行此操作。 The problem is in collect method because at that point of usage doctorsWithManyPatients is of type List<Collection<Person>> and not Collection<Doctor> . 问题出在collect方法中,因为在使用时, doctorsWithManyPatients的类型为List<Collection<Person>>而不是Collection<Doctor> How could I do that? 我怎么能这样做?

Assume that AvgPatientsPerDoctor is already defined somewhere. 假设已经在某处定义了AvgPatientsPerDoctor

You needn't use map ( Doctor -> List<Person> ), it will be used in the filter : 你不需要使用mapDoctor -> List<Person> ),它将在filter

doctors
    .values()
    .stream()
    .filter( d -> d.getPatients().size() >= AvgPatientsPerDoctor)
    .collect(Collectors.toList());

For your case, map( doctor -> doctor.getPatients() ) returns Stream<List<Person>> and you should convert it to Stream<Doctor> again after filter ing and before calling the collect method. 对于您的情况, map( doctor -> doctor.getPatients() )返回Stream<List<Person>> ,您应该在filter之后和调用collect方法之前将其再次转换为Stream<Doctor>


There is a different way that isn't the best one. 有一种不同的方式不是最好的方式。 Keep in mind that it changes the origin collection. 请记住,它会更改原始集合。

doctors.values().removeIf(d -> d.getPatients().size() < AvgPatientsPerDoctor);

如何转换 ArrayList<string> 至 ArrayList<object> using.collect(Collectors.toList() 在 Java stream<div id="text_translate"><p> 如何在 Java stream 中使用.collect(Collectors.toList()将ArrayList&lt;String&gt;转换为ArrayList&lt;Object&gt;</p><p> 之前我用过</p><pre>CommonList = detailUtils.SelectIDValueGetterObservable(getActivity()).stream().forEach(i -&gt; CommonList.add(new foo(i));`</pre><p> 但是我遇到了这些<strong>副作用</strong></p><p> <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html</a></p><p> 这建议.collect(Collectors.toList()</p><p> 我们该怎么做</p></div></object></string> - How can I convert ArrayList<String> to ArrayList<Object> using .collect(Collectors.toList() in Java stream

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java,Streams:如何使用.collect(Collectors.toList())转换表达式 - Java, Streams: how to convert expression with.collect(Collectors.toList()) Java 8 流:myList.stream().map(Foo::bar).collect(Collectors.toList()) 的简写 - Java 8 streams: Shorthand for myList.stream().map(Foo::bar).collect(Collectors.toList()) Java .forEach(list :: add)与.collect(Collectors.toList()) - Java .forEach(list::add) vs .collect(Collectors.toList()) Java 16的Stream.toList()和Stream.collect(Collectors.toList()的区别? - Differences of Java 16's Stream.toList() and Stream.collect(Collectors.toList())? Java Stream Collectors.toList()不会编译 - Java Stream Collectors.toList() wont compile java中Stream Collectors.toList()中的不兼容类型 - Incompatible types in stream Collectors.toList() in java 如何转换 ArrayList<string> 至 ArrayList<object> using.collect(Collectors.toList() 在 Java stream<div id="text_translate"><p> 如何在 Java stream 中使用.collect(Collectors.toList()将ArrayList&lt;String&gt;转换为ArrayList&lt;Object&gt;</p><p> 之前我用过</p><pre>CommonList = detailUtils.SelectIDValueGetterObservable(getActivity()).stream().forEach(i -&gt; CommonList.add(new foo(i));`</pre><p> 但是我遇到了这些<strong>副作用</strong></p><p> <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html</a></p><p> 这建议.collect(Collectors.toList()</p><p> 我们该怎么做</p></div></object></string> - How can I convert ArrayList<String> to ArrayList<Object> using .collect(Collectors.toList() in Java stream 如何在Kotlin中的Java 8 Stream上调用collect(Collectors.toList())? - How can I call collect(Collectors.toList()) on a Java 8 Stream in Kotlin? 为什么 java.lang.OutOfMemoryError: 在尝试打印代码“stream3.collect(Collectors.toList());”时抛出? - Why java.lang.OutOfMemoryError: is thrown when try to print the piece of code "stream3.collect(Collectors.toList());"? Java Stream:如何避免在 Collectors.toList() 中添加 null 值? - Java Stream: How to avoid add null value in Collectors.toList()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM