You could use Spring Spel (org.springframework.expression.ExpressionParser):
public static void main(String[] args) {
List<Emp> emp = Arrays.asList(new Emp("Ann", 25, 1000L)
,new Emp("John", 40, 2000L)
,new Emp("Alex", 60, 3000L));
ExpressionParser parser = new SpelExpressionParser();
Expression exp = parser.parseExpression("age gt 30");
emp.stream()
.filter(emp1 -> exp.getValue(emp1, Boolean.class))
.forEach(emp1 -> System.out.println(emp1.getName() + " " + emp1.getAge()));
}
Output:
John 40
Alex 60
Types of literals and operations are:
- Literals:
- Text literals: 'one text', 'Another one!',…
- Number literals: 0, 34, 3.0, 12.3,…
- Boolean literals: true, false
- Null literal: null
- Literal tokens: one, sometext, main,…
- Text operations:
- String concatenation: +
- Literal substitutions: |The name is ${name}|
- Arithmetic operations:
- Binary operators: +, -, *, /, %
- Minus sign (unary operator): -
- Boolean operations:
- Binary operators: and, or
- Boolean negation (unary operator): !, not
- Comparisons and equality:
- Comparators: >, <, >=, <= (gt, lt, ge, le)
- Equality operators: ==, != (eq, ne)
- Conditional operators:
- If-then: (if) ? (then)
- If-then-else: (if) ? (then) : (else)
- Default: (value) ?: (defaultvalue)