Java 8 Predicate&BiPredicate接口使用
Java8里面Predicate<T>是一个函数式接口,它接受一个参数返回一个boolean类型,通常在集合对象的筛选器中使用,BiPredicate<T, U>和Predicate<T>功能类似,只是多了一个参数。
1、Predicate Hello World
package com.cattles.function;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* @author cattle - 稻草鸟人
* @date 2020/3/5 下午1:33
*/
public class Java8Predicate1 {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Predicate<Integer> predicate1 = x -> x > 3;
List<Integer> collect = list.stream().filter(predicate1).collect(Collectors.toList());
System.out.println(collect);
}
}
输出:
[4, 5, 6, 7, 8, 9, 10]
2、Predicate<T> and 方法使用
下面这个例子最终要取得的值是大于3且小于9的数字,我们看一下代码和结果
package com.cattles.function;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* @author cattle - 稻草鸟人
* @date 2020/3/5 下午3:32
*/
public class Java8Predicate2 {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
Predicate<Integer> predicate1 = x -> x > 3;
Predicate<Integer> predicate2 = x -> x < 9;
List<Integer> collect = list.stream().filter(predicate1.and(predicate2)).collect(Collectors.toList());
System.out.println(collect);
}
}
输出:
[4, 5, 6, 7, 8]
上面是直接使用了Preciate的and方法,我们也可以直接传入 x -> x > 3 & x < 8
得到一样的结果哦
3、Predicate or 方法使用
package com.cattles.function;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* @author cattle - 稻草鸟人
* @date 2020/3/5 下午3:38
*/
public class Java8Predicate3 {
public static void main(String[] args) {
List<String> list = Arrays.asList("java","c++","c","c#","php","kotlin","javascript");
Predicate<String> predicate1 = x -> x.length() > 3;
Predicate<String> predicate2 = x -> x.endsWith("+");
List<String> collect = list.stream().filter(predicate1.or(predicate2)).collect(Collectors.toList());
System.out.println(collect);
}
}
输出:
[java, c++, kotlin, javascript]
4、Predicate negate 方法使用
找出集合中不以“+”结尾的所有集合
package com.cattles.function;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* @author cattle - 稻草鸟人
* @date 2020/3/5 下午3:38
*/
public class Java8Predicate4 {
public static void main(String[] args) {
List<String> list = Arrays.asList("java","c++","c","c#","php","kotlin","javascript");
Predicate<String> predicate2 = x -> x.endsWith("+");
List<String> collect = list.stream().filter(predicate2.negate()).collect(Collectors.toList());
System.out.println(collect);
}
}
输出:
[java, c, c#, php, kotlin, javascript]
5、Predicate test 方法使用
package com.cattles.function;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* @author cattle - 稻草鸟人
* @date 2020/3/5 下午3:38
*/
public class Java8Predicate5 {
public static void main(String[] args) {
List<String> list = Arrays.asList("java", "c++", "c", "c#", "php", "kotlin", "javascript");
Predicate<String> predicate2 = x -> x.endsWith("+");
List<String> collect = list.stream().filter(predicate2::test).collect(Collectors.toList());
System.out.println(collect);
}
}
[c++]
6、 Predicate 链式调用
package com.cattles.function;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* @author cattle - 稻草鸟人
* @date 2020/3/5 下午3:38
*/
public class Java8Predicate6 {
public static void main(String[] args) {
List<String> list = Arrays.asList("java", "c++", "c", "c#", "php", "kotlin", "javascript");
Predicate<String> predicate2 = x -> x.startsWith("c");
//以c开头或者以t结尾
boolean ret = predicate2.or(x -> x.endsWith("t")).test("javascript");
System.out.println(ret);
//!(以c开头且长度等于4)
boolean ret2 = predicate2.and(x -> x.length() == 4).negate().test("java");
System.out.println(ret2);
}
}
输出:
true
true
7、BiPredicate Hello World
package com.cattles.function;
import java.util.function.BiPredicate;
/**
* @author cattle - 稻草鸟人
* @date 2020/3/5 下午3:38
*/
public class Java8BiPredicate1 {
public static void main(String[] args) {
BiPredicate<String, Integer> filter = (x, y) -> {
return x.length() == y;
};
boolean ret = filter.test("cattle", 6);
boolean ret2 = filter.test("java", 8);
System.out.println(ret);
System.out.println(ret2);
}
}
true
false
8、BiPredicate 当作参数使用
找出姓名为mars,年纪大于等于30岁的同学
package com.cattles.function;
import java.util.Arrays;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
/**
* @author cattle - 稻草鸟人
* @date 2020/3/5 下午3:38
*/
public class Java8BiPredicate2 {
public static void main(String[] args) {
Java8BiPredicate2 obj = new Java8BiPredicate2();
Student student1 = new Student("cattle", 18);
Student student2 = new Student("tony", 22);
Student student3 = new Student("mars", 30);
List<Student> students = Arrays.asList(student1, student2, student3);
BiPredicate<Integer, String> biPredicate = (age, name) -> (age >= 30 && name.equals("mars"));
List<Student> ret = filter(students, biPredicate);
System.out.println(ret);
}
public static <T extends Student> List<T> filter(List<T> stu, BiPredicate<Integer, String> biPredicate) {
return stu.stream().filter(x -> biPredicate.test(x.getAge(), x.getName())).collect(Collectors.toList());
}
static class Student {
private String name;
private Integer age;
public Student(String name, Integer age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
}
输出:
[Student{name='mars', age=30}]
Title clip-f000074.png
网友评论