Java – How will you traverse the list in retrolambda?

How will you traverse the list in retrolambda?… here is a solution to the problem.

How will you traverse the list in retrolambda?

I have the following code

List<Trailer> trailers = response.body().getResults();
trailers.stream().forEach(trailer -> {
    View reviewView = LayoutInflater.from(mContext).inflate(R.layout.trailer_list_item, null);
    ((TextView) reviewView.findViewById(R.id.movies_trailers_list)).setText(trailer.getName());
    LinearLayout linearLayout = (LinearLayout) mView.findViewById(R.id.movie_trailers);
    linearLayout.addView(reviewView);
});

Is there a way to get the same code to work with retrolambda?
I need to iterate through the returned list results and add them to the View.

Any suggestions would be appreciated.

Solution

I recommend using streamsupport as an additional library to the Java 8 streaming section:

import java8.util.stream.StreamSupport;

StreamSupport.stream(trailers).forEach(trailer -> {
    View reviewView = LayoutInflater.from(mContext).inflate(R.layout.trailer_list_item, null);
    ((TextView) reviewView.findViewById(R.id.movies_trailers_list)).setText(trailer.getName());
    LinearLayout linearLayout = (LinearLayout) mView.findViewById(R.id.movie_trailers);
    linearLayout.addView(reviewView);
});

The changes required for Java 8 streaming code are typically very small and (in most cases) mechanical.

It has added traction because it also already offers a new Java 9 Stream method takeWhile/dropWhile and all other Java 9 enhancements (especially in Optional and Collectors) and optimizations. You can find the API documentation here

Related Problems and Solutions