Get list of Artists from Album by using streams in JSP?

Solution 1:

No, you can't use streams in jsp. You have two options here:

  1. Do the loop in a traditional way in jsp. The same way you would have done it prior to java 8 streams. Even this is difficult due to the nature of the loop you need, that implies data manipulation. JSP is a presentation techonology, very read-only focused (except for simple variable assignment). You could create a Set with <jsp:useBean> but you would'n have a clean way to add elements to it.

  2. Create a method that returns this for you and call it from jsp:

return a.tracks.stream()
    .flatMap(t -> t.getArtists().stream())
    .distinct()
    .collect(Collectors.toList());

You may have the temptation to create this method directly in the entity class, but I would advice against this. Keep the entity for the entity purpose, and use a Helper class to provide supporting functions to use in jsp.