简体   繁体   中英

Get Pojo from a java list using parallel stream without using any kind of index

I have a Java List as follows:

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. 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.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:

[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:

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)? 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.

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(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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM