我不明白为什么我不能计算名为“empStream”的流中的元素数量。

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

Need to know why I am not able to count the element(s) inside my stream named "empStream"

问题

以下是您要翻译的内容:

我正在传递一个列表来创建/生成一个流,并计算该流的元素数量。我理解我的“empStream”只包含一个元素,即Employee列表。根据Java文档,一旦流被消耗/使用,我们就不能对其执行其他操作。但是在“empStream.count();”语句之前,我没有在任何地方使用empStream。
那么为什么“empStream.count();”没有被执行呢?

public class AAAproblem1 {
     public static void main(String[] args) {
         List<Employee> list = new ArrayList<>();
         list.add(new Employee(6, "Nick", 27, "Software Engineer"));
         list.add(new Employee(9, "Tom", 23, "Civil Engineer"));
         list.add(new Employee(3, "Jon", 29, "Mechanical Engineer"));
         list.add(new Employee(4, "Harry", 21, "Surgeon"));
         list.add(new Employee(8, "Don", 25, "Laywer"));
         list.add(new Employee(7, "Marry", 20, "Police"));
         list.add(new Employee(2, "Angel", 22, "Professor"));
         list.add(new Employee(1, "Kate", 23, "Teacher"));
         list.add(new Employee(5, "Evan", 22, "Pilot"));

         generateStream(list);
    }

    private static void generateStream(List<Employee> list) {
        Stream<List<Employee>> empStream = Stream.generate(() -> {
            return list;
        });
       empStream.onClose(()->{System.out.println("empStream is closed");});
       System.out.println("counting number of list inside empStream started");
       // not able to count the elements inside emp stream
       long items = empStream.count();
       System.out.println("counting number of list inside empStream finished");
       System.out.println("The number of employee list in streams is/are - " + items);
       empStream.onClose(()->{System.out.println("empStream is closed");});
    }
}
英文:

I am passing a list to create/generate a stream and count the elements of that stream. What i understand is that my "empStream" contains only one element which is list of Employee. According to java docs the stream is closed once it gets consumed/used and we cannot perform other operations on that stream.But here i have not consumed empStream anywhere before "empStream.count();" statement.
So why "empStream.count();" is not getting executed.

 public class AAAproblem1 {
public static void main(String[] args) {
List&lt;Employee&gt; list = new ArrayList&lt;&gt;();
list.add(new Employee(6, &quot;Nick&quot;, 27, &quot;Software Engineer&quot;));
list.add(new Employee(9, &quot;Tom&quot;, 23, &quot;Civil Engineer&quot;));
list.add(new Employee(3, &quot;Jon&quot;, 29, &quot;Mechanical Engineer&quot;));
list.add(new Employee(4, &quot;Harry&quot;, 21, &quot;Surgeon&quot;));
list.add(new Employee(8, &quot;Don&quot;, 25, &quot;Laywer&quot;));
list.add(new Employee(7, &quot;Marry&quot;, 20, &quot;Police&quot;));
list.add(new Employee(2, &quot;Angel&quot;, 22, &quot;Professor&quot;));
list.add(new Employee(1, &quot;Kate&quot;, 23, &quot;Teacher&quot;));
list.add(new Employee(5, &quot;Evan&quot;, 22, &quot;Pilot&quot;));
generateStream(list);
}
private static void generateStream(List&lt;Employee&gt; list) {
Stream&lt;List&lt;Employee&gt;&gt; empStream = Stream.generate(() -&gt; {
return list;
});
empStream.onClose(()-&gt;{System.out.println(&quot;empStream is closed&quot;);});
System.out.println(&quot;counting number of list inside empStream started&quot;);
// not able to count the elements inside emp stream
long items = empStream.count();
System.out.println(&quot;counting number of list inside empStream finished&quot;);
System.out.println(&quot;The number of employee list in streams is/are - &quot; + items);
empStream.onClose(()-&gt;{System.out.println(&quot;empStream is closed&quot;);});
}
}
```

答案1

得分: 0

Stream.generate 通过调用供应商返回一个无限顺序无序流。在这种情况下,生成的流是一个无限流,其中每个元素都是列表。

您不能对无限流应用 count 操作。您需要一些短路操作(比如 limit)来将 generate 方法生成的流转换为有限流(如此处所示)。

在这里,您需要使用 list.stream().count()

英文:

Stream.generate returns an infinite sequential unordered stream by calling the supplier. In this case, the stream produced is an infinite stream where each element is the list.

You cannot apply the count operation to an infinite stream. You need some short-circuiting operation (like limit) to convert the stream generated by the generate method to a finite stream (as shown here)

list.stream().count() is what you want here.

huangapple
  • 本文由 发表于 2020年10月3日 14:39:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64181440.html
匿名

发表评论

匿名网友

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

确定