1、四大核心函数式接口
-
消费型
void accept(T t)
-
供给型
T get()
-
函数型
R get(T t)
-
断定式
boolean test(T t)
//举例:
public class Lambda01 {
@Test
public void test(){
happy(500.00, new Consumer<Double>() {
@Override
public void accept(Double aDouble) {
System.out.println("HHH" + aDouble);
}
});
System.out.println("=========================");
happy(400.00,money -> System.out.println("YYY"));
}
public void happy(Double money,Consumer<Double> consumer){
consumer.accept(money);
}
@Test
public void test1(){
List<String> list = Arrays.asList("a","ab","abc","d" );
List<String> strings = filter(list, s -> s.contains("d"));
System.out.println(strings);
}
public List<String> filter(List<String> list,Predicate<String> predicate){
ArrayList<String> list1 = new ArrayList<>();
list.forEach( str ->{
if (predicate.test(str)){
list1.add(str);
}
});
return list1;
}
}
2、方法的引用
-
使用情景:当要传给Lambda体的操作,已经有实现方法的时候,可以使用方法引用。
-
方法引用:本质上就是lambda表达式,而lambda表达式作为函数式接口的实例,所以方法引用也是函数式接口的实例。
-
使用格式:类(对象)::方法名
-
具体分为下面三种:
- 对象::非静态方法
- 类::静态方法
- 类::非静态方法
-
使用要求:要求接口中的抽象方法的形参列表和返回值类型与方法引用的方法的形参列表和返回值相同。
比如:Consummer 中的void accept(T t)和PrintStream 中的void print(T t)
-
举例:
public class FfYinYong { @Test public void test(){ Consumer<String> consumer = str -> System.out.println(str); consumer.accept("HHH"); System.out.println("======================"); PrintStream ps = System.out; Consumer<String> consumer1 = ps :: println; consumer1.accept("YYY"); } @Test public void test1(){ Comparator<Integer> comparator = (t1,t2) -> Integer.compare(t1,t2); System.out.println(comparator.compare(1, 2)); System.out.println("=============================="); Comparator<Integer> comparator1 = Integer::compare; System.out.println(comparator1.compare(0, 0)); } }
3、强大的Stream API
-
集合关注的是存储,Stream关注的是计算
-
执行流程:实例化 --> 一系列的中间操作(过滤,映射)–> 终止操作
一旦执行终止操作,就执行中间操作链,并产生结果。
-
stream流的获取
public class StreamAPI { /** * @Description: 创建stream的方式 * @Auther: pss * @Date: 2019/9/16 21:25 */ @Test public void test() { Emp emp1 = new Emp(1, "a", 1); Emp emp2 = new Emp(2, "b", 2); Emp emp3 = new Emp(3, "c", 3); Emp emp4 = new Emp(4, "d", 4); Emp emp5 = new Emp(5, "e", 5); ArrayList<Emp> emps = new ArrayList<>(); emps.add(emp1); emps.add(emp2); emps.add(emp3); emps.add(emp4); emps.add(emp5); //返回一个顺序流 Stream<Emp> stream = emps.stream(); //返回一个并行流 Stream<Emp> parallelStream = emps.parallelStream(); } @Test public void test2() { int[] arr = new int[]{0, 1, 2, 3}; //调用Arrays的stream方法返回一个流 IntStream stream = Arrays.stream(arr); } @Test public void test3(){ //直接创建 Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5); } }
-
stream的中间操作
-
筛选与切片
public class StreamAPI02 { @Test public void test() { Emp emp1 = new Emp(1, "a", 1); Emp emp2 = new Emp(2, "b", 2); Emp emp3 = new Emp(3, "c", 3); Emp emp4 = new Emp(4, "d", 4); Emp emp5 = new Emp(5, "e", 5); Emp emp6 = new Emp(5, "e", 5); Emp emp7 = new Emp(5, "e", 5); ArrayList<Emp> emps = new ArrayList<>(); emps.add(emp1); emps.add(emp2); emps.add(emp3); emps.add(emp4); emps.add(emp5); // filter(Predicate p)--接收lambda,从流中排除某些元素 Stream<Emp> stream = emps.stream(); stream.filter( e -> e.getAge() > 2).forEach(e -> System.out.println(e)); System.out.println("============================"); // limit(n)--截断流,使其不超过规定的数量 emps.stream().limit(3).forEach(e -> System.out.println(e)); System.out.println("============================"); // skip(3)--跳过给定数量的元素 emps.stream().skip(2).forEach(e -> System.out.println(e)); System.out.println("============================"); // distinct()--筛选,通过流所生成元素的hashcode()和equals()方法去重 emps.stream().distinct().forEach(e -> System.out.println(e)); } @Test public void test2() { int[] arr = new int[]{0, 1, 2, 3}; //调用Arrays的stream方法返回一个流 IntStream stream = Arrays.stream(arr); } @Test public void test3(){ //直接创建 Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5); } }
-
映射
@Test public void test2() { // map(function f) --接收一个函数作为参数,将元素转换成其他形式的信息 List<String> list = Arrays.asList("aa", "bb", "cc", "dd"); list.stream().map(str -> str.toUpperCase()).forEach(System.out::println); list.stream().map(str -> str.toUpperCase()).forEach(str -> System.out.print(str + "-")); System.out.println(); System.out.println("============================"); // 练习:获取年龄大于2的人员 Emp emp1 = new Emp(1, "a", 1); Emp emp2 = new Emp(2, "b", 2); Emp emp3 = new Emp(3, "c", 3); Emp emp4 = new Emp(4, "d", 4); Emp emp5 = new Emp(5, "e", 5); Emp emp6 = new Emp(5, "e", 5); Emp emp7 = new Emp(5, "e", 5); ArrayList<Emp> emps = new ArrayList<>(); emps.add(emp1); emps.add(emp2); emps.add(emp3); emps.add(emp4); emps.add(emp5); //emps.stream().map(emp -> emp.getAge() > 2).forEach(emp -> System.out.println(emp)); Stream<Integer> stream = emps.stream().map(Emp::getAge); stream.filter(age -> age >2).forEach(e -> System.out.println(e)); }
-
排序
@Test public void test3(){ // 排序sorted()自然排序 List<Integer> list = Arrays.asList(1, 2, 3, 4, 7, 5, 4); list.stream().sorted().forEach( e -> System.out.println(e)); System.out.println("========================"); // sorted(Comparator c)--定制排序 Emp emp1 = new Emp(1, "a", 1); Emp emp2 = new Emp(2, "b", 2); Emp emp3 = new Emp(3, "c", 3); Emp emp4 = new Emp(4, "d", 4); Emp emp5 = new Emp(5, "e", 5); Emp emp6 = new Emp(5, "e", 5); Emp emp7 = new Emp(5, "e", 5); ArrayList<Emp> emps = new ArrayList<>(); emps.add(emp1); emps.add(emp2); emps.add(emp3); emps.add(emp4); emps.add(emp5); emps.add(emp6); emps.add(emp7); emps.stream().sorted((e1,e2) -> Integer.compare(e1.getAge(),e2.getAge())).forEach(System.out::println); }
-
-
终止操作
-
匹配与查找
public class StreamAPI03 { /** * @Description: 终止操作 * @Auther: pss * @Date: 2019/9/16 21:25 */ @Test public void test() { Emp emp1 = new Emp(1, "a", 1); Emp emp2 = new Emp(2, "b", 2); Emp emp3 = new Emp(3, "c", 3); Emp emp4 = new Emp(4, "d", 4); Emp emp5 = new Emp(5, "e", 5); ArrayList<Emp> emps = new ArrayList<>(); emps.add(emp1); emps.add(emp2); emps.add(emp3); emps.add(emp4); emps.add(emp5); // allMatch()--检查是否匹配所有元素 boolean b = emps.stream().allMatch(e -> e.getAge() > 2); System.out.println("=============="+b); System.out.println("==========================="); // anyMatch()--检查至少一个元素匹配 boolean anyMatch = emps.stream().anyMatch(e -> e.getAge() > 3); System.out.println(anyMatch); System.out.println("==========================="); // noneMatch()--检查是否没有匹配的元素 boolean noneMatch = emps.stream().noneMatch(emp -> emp.getName().startsWith("a")); System.out.println(noneMatch); System.out.println("==========================="); // findFirst()--返回第一个元素 Optional<Emp> first = emps.stream().findFirst(); // findAny()--返回任意元素 Optional<Emp> any = emps.stream().findAny(); // count()--返回元素个数 long count = emps.stream().count(); // min(Comparator c)--返回最小值的员工 Optional<Emp> min = emps.stream().min((e1, e2) -> Double.compare(e1.getAge(), e2.getAge())); // forEach(Consumer c)--内部迭代,直接遍历 emps.stream().forEach(System.out::println); } @Test public void test2() { int[] arr = new int[]{0, 1, 2, 3}; //调用Arrays的stream方法返回一个流 IntStream stream = Arrays.stream(arr); } @Test public void test3(){ //直接创建 Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5); } }
-
规约
@Test public void test3(){ // reduce(T identity,BinaryOperator)--可以将流中元素反复结合起来,得到一个值 // 计算1到5的和 List<Integer> list = Arrays.asList(0, 1, 2, 3, 4, 5); Integer sum = list.stream().reduce(0, Integer::sum); System.out.println(sum); // 计算所有员工的工资和 Emp emp1 = new Emp(1, "a", 1); Emp emp2 = new Emp(2, "b", 2); Emp emp3 = new Emp(3, "c", 3); Emp emp4 = new Emp(4, "d", 4); Emp emp5 = new Emp(5, "e", 5); ArrayList<Emp> emps = new ArrayList<>(); emps.add(emp1); emps.add(emp2); emps.add(emp3); emps.add(emp4); emps.add(emp5); Optional<Integer> reduce = emps.stream().map(Emp::getAge).reduce(Integer::sum); }
-
收集
@Test public void test() { Emp emp1 = new Emp(1, "a", 1); Emp emp2 = new Emp(2, "b", 2); Emp emp3 = new Emp(3, "c", 3); Emp emp4 = new Emp(4, "d", 4); Emp emp5 = new Emp(5, "e", 5); ArrayList<Emp> emps = new ArrayList<>(); emps.add(emp1); emps.add(emp2); emps.add(emp3); emps.add(emp4); emps.add(emp5); // collect(Collector c)--将流转换为其他形式 // 查找年龄大于2的员工,返回一个List List<Emp> list = emps.stream().filter(e -> e.getAge() > 2).collect(Collectors.toList()); list.forEach(e -> { System.out.println(e); }); }
-