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?

2
  • Did you implemented toString() in your FilePojo ? when you implement toString(), it will populate values of the object Commented Dec 3, 2018 at 12:23
  • Have you read documentation for Stream.of() method? Commented Dec 3, 2018 at 12:25

3 Answers 3

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?

Sign up to request clarification or add additional context in comments.

2 Comments

I'd avoid the last suggestion though, your first is the one I'd suggest.
@Aomine True, but that's just to clarify to the OP on what they've ended up in their code.
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()));

Comments

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.

1 Comment

forEach has a print statement missing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.