2

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?

Naman
  • 27,789
  • 26
  • 218
  • 353
Roshan Upreti
  • 1,782
  • 3
  • 21
  • 34

3 Answers3

1

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()));
Ulad
  • 1,083
  • 8
  • 17
1

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?

Naman
  • 27,789
  • 26
  • 218
  • 353
1

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.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126