说明
match用于匹配操作,其返回值为boolean类型。通过match,可以简单地验证list中是否存在某种要素。
实例
// 验证 list 中 string 是否有以 a 开头的, 匹配到第一个,即返回 true boolean anyStartsWithA = stringCollection .stream() .anyMatch((s) -> s.startsWith("a")); System.out.println(anyStartsWithA); // true // 验证 list 中 string 是否都是以 a 开头的 boolean allStartsWithA = stringCollection .stream() .allMatch((s) -> s.startsWith("a")); System.out.println(allStartsWithA); // false // 验证 list 中 string 是否都不是以 z 开头的, boolean noneStartsWithZ = stringCollection .stream() .noneMatch((s) -> s.startsWith("z")); System.out.println(noneStartsWithZ); // true
以上就是Match在java中的匹配,希望对大家有所帮助。更多Java学习指路:
推荐操作环境:windows7系统、java10版,DELL G3电脑。
Copyright © 2019- hids.cn 版权所有 赣ICP备2024042780号-1
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务