How to extract list of single property of objects in list using SpEL?

Abhay :

I want just list of id from the Tester object list using SpEL

List<Tester> tests = new ArrayList<Tester>();
tests.add(new Tester(1)); ...
ExpressionParser parser = new SpelExpressionParser();
StandardEvaluationContext context = new StandardEvaluationContext();
context.setVariable("tests",tests);
System.out.println(tests.stream().map(Tester::getId).collect(Collectors.toList())); // LIKE THIS
System.out.println(parser.parseExpression("#tests what to write here").getValue(context));

Desired Result : [1, 2, 3, 4]

Tester is

public class Tester {
        private Integer id;
    }
Grzegorz Oledzki :

You can use (what they call) Collection Projection (also known as map in functional programming world):

tests.![id]

Look at the Spring docs for SpEL for reference.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=95294&siteId=1