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?