Lambda表达式与函数式接口与Stream

Lambda
JDK8开始支持Lambda表达式,用来让程序编写更优雅
利用Lambda可以更简洁的实现匿名内部类与函数声明与调用
基于Lambda提供stream流式处理极大简化对集合的操作
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
创建接口

@FunctionalInterface
public interface MathOperation {
    public Float operate(Integer a, Integer b);
}
		//传统方法
        MathOperation addition2 = new MathOperation() {
            @Override
            public Float operate(Integer a, Integer b) {
                return a + b + 0f;
            }
        };
        System.out.println(addition2.operate(5, 4));
 		// 1.标准使用方法
        MathOperation addition = (Integer a, Integer b) -> {
            System.out.println("加法运算");
            return a + b + 0f;
        };
        System.out.println(addition.operate(5, 3));

		// 2.Lambda允许忽略参数类型
        MathOperation subtraction = (a, b) -> {
            System.out.println("减法运算");
            return a - b + 0f;
        };
        System.out.println(subtraction.operate(5, 3));

        // 3.单行实现代码可以省略大括号和return
        MathOperation multiplication = (a, b) -> a * b + 0f;
        System.out.println("执行乘法");
        System.out.println(multiplication.operate(5, 4));

函数式编程
函数式编程是基于函数式接口并使用lambda表达的编程方式
函数式编程理念是将代码作为可重用数据代入到程序运行中
函数式编程强调"你想做什么",而不是"你想怎么做"
函数式接口是有且只有一个抽象方法的接口
Java中拥有大量函数式接口,如java.lang.Runnable
JDK8后提供了一系列新的函数式接口,位于java.util.function

函数式接口Predicate
Predicate是新增的函数式接口,位于java.util.function
Predicate用于测试传入的数据是否满足判断要求
Predicate接口需要实现test()方法进行逻辑判断

   		Predicate<Integer> predicate = n -> n > 4;
        boolean result = predicate.test(10);
        System.out.println(result);
 public static void main(String[] args) {
        // 筛选奇数
        filter(list, n -> n % 2 == 1);
        // 筛选偶数
        filter(list, n -> n % 2 == 0);
        // 大于5的数
        filter(list, n -> n > 5 && n % 2 == 0);
}
 public static void filter(List<Integer> list, Predicate<Integer> predicate) {
        for (Integer num :
                list) {
            if (predicate.test(num)) {
                System.out.print(num + " ");
            }
        }
        System.out.println();
    }

JDK8常用函数式接口

接口用途
Consumer对应有一个输入参数无输出的功能代码
Function<T,R>对应有一个输入参数并且需要返回数据的功能代码
Predicate用于条件判断,返回布尔值

Consumer函数式接口

public static void main(String[] args) {
        output(s -> System.out.println("向控制台打印:" + s));
        output(s -> {
            System.out.println("向xxx_网站发送数据包:" + s);
        });
    }
    public static void output(Consumer<String> consumer) {
        String text = "好好学习";
        consumer.accept(text);
    }

Function函数式接口

public static void main(String[] args) {
        Function<Integer, String> randomStringFunction = l -> {
            String chars = "abc123";
            StringBuilder stringBuffer = new StringBuilder();
            Random random = new Random();
            for (int i = 0; i < l; i++) {
                int position = random.nextInt(chars.length());
                stringBuffer.append(chars.charAt(position));
            }
            return stringBuffer.toString();
        };
        System.out.println(randomStringFunction.apply(32));
    }

在接口上添加注解

@FunctionalInterface // 通知编译器这是函数式接口
public interface MathOperation {
    public Float operate(Integer a, Integer b);
}

Stream流式处理
Stream流式处理是建立在Lambda基础上的多数据处理技术
Stream对集合数据处理进行高度抽象,极大简化代码量
Stream可对集合进行迭代,去重,筛选,排序,聚合等一系列处理

Stream常用方法

接口用途
forEach循环遍历
mapmap 方法用于映射每个元素到对应的结果
filterfilter 方法用于对通过的设置条件过滤除元素
limitlimit 方法用于获取指定数量的流
sortedsorted 方法用于对流进行排序
CollectorsCollectors 类实现将流转换成集合和聚合元素

Stream流的五种创建方式

public class StreamGenerator {
    // 基于数组进行创建
    @Test
    public void generator1() {
        String[] arr = {"李莉", "安迪", "史蒂斯"};
        Stream<String> stream = Stream.of(arr);
        stream.forEach(System.out::println);
    }

    // 基于集合进行创建
    @Test
    public void generator2() {
        List<String> list = new ArrayList<>();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        Stream<String> stream = list.stream();
        stream.forEach(System.out::println);
    }

    // 创建无限长度流
    @Test
    public void generator3() {
        Stream<Integer> stream = Stream.generate(() -> new Random().nextInt(10000) + 1);
        stream.limit(100).forEach(System.out::println);
    }

    // 基于迭代器创建流
    @Test
    public void generator4() {
        Stream<Integer> stream = Stream.iterate(1, n -> n + 1);
        stream.limit(20).forEach(System.out::println);
    }

    // 基于字符序列创建流
    @Test
    public void generator5() {
        String str = "abcdefg";
        IntStream stream = str.chars();
        stream.forEach(c -> System.out.println((char) c));
    }

Stream常用方法

public class StreamMethod {
    // 提取集合中所有的偶数并求和
    @Test
    public void case1() {
        List<String> list = Arrays.asList("1", "2", "3", "4", "5", "6", "7");
        int sum = list.stream()
                .mapToInt(s -> Integer.parseInt(s))
                .filter(n -> n % 2 == 0)
                .sum();//获取stream对象
        System.out.println(sum);
    }

    // 所有名字首字母大写
    @Test
    public void case2() {
        List<String> list = Arrays.asList("lily", "jack", "tom");
        List<String> newList = list.stream()
                // 按规则对每一个流数据进行转换
                .map(s -> s.substring(0, 1).toUpperCase() + s.substring(1))
//                .forEach(System.out::println)
                // collect对数据进行手机,生成新的List/Set
                .collect(Collectors.toList());
        System.out.println(newList);
    }

    // 将所有奇数从大到小进行排序,切不许出现重复
    @Test
    public void case3() {
        List<Integer> list = Arrays.asList(1, 34, 76, 345433, 234, 2, 4, 6, 1, 43, 1, 2, 345, 3);
        List<Integer> newList = list.stream()
                // 去除重复的流数据
                .distinct()
                .filter(n -> n % 2 == 1)
                .sorted((a, b) -> b - a)
                .collect(Collectors.toList());
        System.out.println(newList);
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值