[英]Get Pojo from a java list using parallel stream without using any kind of index
I have a Java List as follows: 我有一个Java列表,如下所示:
FileService fileService = new FileService("testi");
List<FilePojo> list = fileService.getAllFiles();
What I want to do is iterate over the list, get FilePojo
object from it and print certain properties. 我想做的是遍历列表,从中获取FilePojo
对象并打印某些属性。 I can achieve that by doing something like this: 我可以通过执行以下操作来实现:
for (FilePojo filePojo : list){
System.out.println(filePojo.getId()+" "+filePojo.getName());
}
And then I stumbled across Stream api and tried refactoring my code as follows: 然后我偶然发现了Stream api,并尝试如下重构我的代码:
Stream.of(list).parallel().forEach(filePojo -> {
System.out.println(filePojo);
}
});
Using this, I cannot access the getters from filePojo (Unlike the first method), although I can see the objects like this: 使用此方法,我无法从filePojo访问getter(与第一种方法不同),尽管我可以看到如下所示的对象:
[pojo.FilePojo@56528192, pojo.FilePojo@6e0dec4a, pojo.FilePojo@96def03, pojo.FilePojo@5ccddd20, pojo.FilePojo@1ed1993a, pojo.FilePojo@1f3f4916, pojo.FilePojo@794cb805, pojo.FilePojo@4b5a5ed1]
I can access getters if I use an index like this: 如果使用这样的索引,则可以访问getter:
Stream.of(list).parallel().forEach(filePojo -> {
for (int i = 0; i < list.size(); i++) {
System.out.println("=============================\n" + "Element at " + i + "\n" + filePojo.get(i).getId() + "\n" + filePojo.get(i).getCustomerId() + "\n" + filePojo.get(i).getFileName() + "\n ========================");
}
});
Is it possible to get the Pojo from the stream of a list without using an index (similar to the first method)? 是否可以在不使用索引的情况下从列表流中获取Pojo(类似于第一种方法)? or do I need to resort to indexing to solve this problem? 还是我需要借助索引来解决此问题?
You are using Stream.of() method in a wrong way, you are creating stream of lists, not of objects inside of it. 您以错误的方式使用Stream.of()方法,正在创建列表流,而不是其中的对象流。
Try this one: 试试这个:
list.stream()
.parallel()
.forEach(filePojo -> System.out.println(filePojo.getId() + " " + filePojo.getName()));
You can do it using streams as: 您可以使用流来执行以下操作:
list.stream() // Stream<FilePojo>
.map(filePojo -> filePojo.getId() + " " + filePojo.getName())
.forEach(System.out::println);
Using the Stream.of
, you would have to flatMap
the stream since you end up creating the Stream<List<FilePojo>>
instead as 使用Stream.of
,你就必须flatMap
,因为你最终创建流Stream<List<FilePojo>>
而不是作为
Stream.of(list) // Stream<List<FilePojo>>
.flatMap(List::stream) // Stream<FilePojo>
.map(filePojo -> filePojo.getId() + " " + filePojo.getName())
.forEach(System.out::println);
Further read : Should I always use a parallel stream when possible? 进一步阅读 : 如果可能,是否应该始终使用并行流?
Sure, you can create a "stream", map it then print: 当然,您可以创建一个“流”,将其映射然后打印:
list.stream()
.map(filePojo -> filePojo.getId() + " " + filePojo.getName())
.forEach(e -> System.out.println(e));
Stream.of(list)
reads as "create a stream of lists" but what you want is "create a stream of elements in the list" hence the list.stream
above. Stream.of(list)
读为“创建列表流”,但是您想要的是“在列表中创建元素流”,因此list.stream
上面的list.stream
。
Further, don't carelessly call Stream.of(list).parallel()
when you're not sure that you can benefit from parallel processing, it might come back to bite you with worse performance than the sequential approach. 此外,当您不确定是否可以从并行处理中受益时,请不要粗心地调用Stream.of(list).parallel()
,它可能会比顺序方法带来更差的性能。
but in reality if all you need is the above then there is no need for this, just proceed with your approach or do: 但是实际上,如果您需要的只是上述内容,那么就不需要这样做,只需继续执行您的方法或执行以下操作:
list.forEach(f -> System.out.println(f.getId()+" "+f.getName()));
since lists have the forEach
method. 因为列表具有forEach
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.