Java lambda sublist

2019-06-19 01:57发布

What is the shortest way to express "get new List B from List A where condition" via a Java 8 lambda?

Say I have List<Integer> a = Arrays.asList(1, 2, 3, 4, 5) and I want a new List, B, where the value is > 3.

I've read through the new Collections Streams API, but I'm not convinced I have found the best way to do this, and don't want to taint the question with what is probably my less than perfect solution.

1条回答
神经病院院长
2楼-- · 2019-06-19 02:53
a.stream().filter(x -> x > 3).collect(Collectors.toList());
查看更多
登录 后发表回答