1

I have a list of object and I want to get a list from its field.
Say I pass List<Auto> autos from java side to the template on the document side. Each Auto has a field of speed. So the result should be list of speed.
I can do it manually looping through the autos and building a new list from the speed fields. Is there any easier solution built in for this in the freemarker something like 'autos.speed?tolist'

1 Answer 1

1

You are looking for the sequence built-in map, which is available from version 2.3.29. It returns a new sequence where all elements are replaced with the result of the parameter lambda, function, or method. This allows you to do:

autos?map(auto -> auto.speed)

If you want to do this in Java, see:

There it comes down to:

autos.stream().map(Auto::getSpeed).collect(Collectors.toList());
Sign up to request clarification or add additional context in comments.

Comments

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.