英文:
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<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");});
}
}
```
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论