关于Java中Stream的用法 (1)
Java集合Stream 出自 Java8。
可谓是加量不加价,丰富了使用场景,还精简了代码。虽然牺牲了一点可读性,但总体来说是很好用的。以下总结其中 filter 的一些用法,整理相关的例子。
stream().filter匹配过滤
一、简单匹配过滤
简单对象
1 1 public class TestFilter {
2 2 public static void main(String[] args) {
3 3
4 4 List<String> list = Arrays.asList("AAB","BBB","ACC");
5 5
6 6 System.out.println("过滤相等-------------------------------------------");
7 7 List<String> result1 = list.stream().filter(v -> v.equals("AAB")).collect(Collectors.toList());
8 8 result1.forEach(v-> System.out.println(v));
9 9
10 10 System.out.println("过滤 模糊相等(包含)-------------------------------------------");
11 11 List<String> result2 = list.stream().filter(v -> v.contains("B")).collect(Collectors.toList());
12 12 result2.forEach(v-> System.out.println(v));
13 13
14 14 System.out.println("多条件 过滤 与:&&; 或||-------------------------------------------");
15 15 List<String> result3 = list.stream().filter(v -> v.contains("A") && v.contains("C")).collect(Collectors.toList());
16 16 result3.forEach(v-> System.out.println(v));
17 17 }
18 18 }
运行结果:
可见,filter支持单条件,多条件组合匹配过滤
二、对象匹配过滤
假设对象:
1 public class User {
2
3 /** 名称 */
4 private String name;
5
6 /** 年龄 */
7 private Integer age;
8 }
例子:
1 public class TestFilter1 {
2 public static void main(String[] args) {
3
4 List<User> userList = new ArrayList<>();
5 userList.add(new User("小明", 18));
6 userList.add(new User("王姐", 21));
7
8 System.out.println("过滤相等-------------------------------------------");
9 List<User> result1 = userList.stream().filter(v -> v.getName().equals("小明")).collect(Collectors.toList());
10 result1.forEach(v-> System.out.println(v));
11 }
12 }
三、自定义匹配过滤
filter接口的源码:
1 public interface Stream<T> extends BaseStream<T, Stream<T>> {
2
3 /**
4 * Returns a stream consisting of the elements of this stream that match
5 * the given predicate.
6 *
7 * <p>This is an <a href="package-summary.html#StreamOps">intermediate
8 * operation</a>.
9 *
10 * @param predicate a <a href="package-summary.html#NonInterference">non-interfering</a>,
11 * <a href="package-summary.html#Statelessness">stateless</a>
12 * predicate to apply to each element to determine if it
13 * should be included
14 * @return the new stream
15 */
16 Stream<T> filter(Predicate<? super T> predicate);
可以看到,filter接收的参数是Predicate,这是一个布尔接口,用于各种匹配,所以自定义filter实际上我们是你把Predicate的用法丰富起来
比如:
1 public class TestFilter1 {
2 public static void main(String[] args) {
3
4 List<User> userList = new ArrayList<>();
5 userList.add(new User("小明", 18));
6 userList.add(new User("王姐", 21));
7
8 System.out.println("过滤相等-------------------------------------------");
9 List<User> result2 = userList.stream().filter(new Predicate<User>() {
10 @Override
11 public boolean test(User user) {
12 if (user.getAge() > 18) {
13 return true;
14 }else{
15 return false;
16 }
17 }
18 }).collect(Collectors.toList());
19 result2.forEach(v-> System.out.println(v));
20 }
21 }
运行结果:

参考链接:
Java 8 Stream :https://www.runoob.com/java/java8-streams.html
Java集合Stream类filter的使用:https://blog.csdn.net/qq_33829547/article/details/80279488
stream().filter匹配过滤总结:https://blog.csdn.net/zhan107876/article/details/117329626

浙公网安备 33010602011771号