简体   繁体   English

使用并行流从Java列表中获取Pojo,而无需使用任何类型的索引

[英]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.

相关问题 如何使用Java Stream从列表中获取任何数据的特定索引? - How to get particular index for any data from list using Java Stream? 如何从 java 中的 class object 列表中获取第一个匹配元素的索引(不使用任何第三方库) - How to get index of first matched element from the list of class object in java (Without using any third party Library) 使用java stream获取arrayList中min size列表的索引 - get the index of the min size list in an arrayList using java stream 在java 8中不使用POJO进行分组 - Grouping By without using a POJO in java 8 不使用java8 Stream从数据库中过滤列表 - Filtering list from database without using java8 Stream 获取列表中包含“.txt”的索引而不使用 JAVA 中的循环 - Get index that contains “.txt” in the list without using loop in JAVA 收集为 HashMap<string,string> 来自 java 中没有 POJO 的对象的列表或 Stream</string,string> - Collect as HashMap<String,String> from a List or Stream of objects without a POJO in java 8 想在 java 8 中使用并行 stream 从两个大数据列表中查找匹配元素 - Want to find matching elements from two large data list using parallel stream in java 8 从Java pojo生成xml而不使用注释 - Generate xml from Java pojo without using annotations 使用 java 流从 List 中获取素数 - get prime numbers from List using java stream
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM