函数式编程

一、Lamda表达式(匿名函数)

  1、定义:Lambda 表达式实际上是一种匿名方法,无名称,返回值

        //正常创建线程
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("normal create thread");
            }
        }).start();

        //Lambda 创建线程
        new Thread(()-> System.out.println("lambda create thread")).start();
    }

二、函数式接口

 1、定义:只有一个抽象方法接口,一般接口上添加@FunctionalInterface注解(非必须)

    @FunctionalInterface
    private interface FunctionApi{
        void testApi();

    }
    public static void main(String[] args) {
        FunctionApi funcApi=()-> System.out.println("测试函数接口");
        funcApi.testApi();
    }

2、常用的函数式接口(java.util.function包下)

        //Supplier 没有输入 只有一个输出
        Supplier<String> supplier = () -> "this is supplier";
        System.out.println(supplier.get());

        //Consumer 只有一个输入 没有输出
        Consumer<Integer> consumer = i -> System.out.println("this is consumer ,param is " + i);
        consumer.accept(100);

        //Function 输入T 输出R
        Function<Integer, Integer> function = t -> t * 10;
        System.out.println(function.apply(10));

        //UnaryOperator 输入输出都是T (一元运算)
        UnaryOperator<Integer> unaryOperator = t -> t * t;
        System.out.println(unaryOperator.apply(9));

        //BiFunction 输入T,U 输出R
        BiFunction<Integer, Integer, String> biFunction = (t, u) -> t + "+" + u + "=" + (t + u);
        System.out.println(biFunction.apply(2, 3));

三、Stream编程

1、定义:流(Stream)是一连串的元素序列,可以进行各种操作以实现数据的转换和处理

2、流创建

        String[] array = {"java", "C#", "VB", "go", "C", "C++", "python","","vb"};
        //1、数组
        Arrays.stream(array).forEach(System.out::println);
        //2、List
        Arrays.asList(array).stream().forEach(System.out::println);
        //3、Stream.of
        Stream.of(array).forEach(System.out::println);
        //4、Stream.iterate
        Stream.iterate(1, i -> i + 1).limit(10).forEach(System.out::println);
        //5、System.generate
        Stream.generate(() -> new Random().nextInt(10)).limit(10).forEach(System.out::print);

3、流操作

  •      中间操作(可以有多个)
    • 无状态:filter() map() flatMap() peek()
    • 有状态:distinct() sorted() limit() skip()
  •      终止操作(只能有一个)
    • 非短路操作:forEach() toArray() collect() reduce() min()/max() count()
    • 短路操作   :findFirst()/findAny()   anyMatch()/allMatch()/noneMatch()
        String[] array = {"java", "C#", "VB", "go", "C", "C++", "python", "", "vb"};
        //排序去重
        Stream.of(array)
                .filter(i -> !i.isEmpty())
                .peek(i-> System.out.println(i))
                .map(i -> i.toUpperCase())
                .distinct()
                .sorted()
                .collect(Collectors.toList())
                .forEach(System.out::println);


        List<String> list1 = Arrays.asList("apple", "bag", "led");
        List<String> list2 = Arrays.asList("toy", "boy", "tree");
        //map : list不合并
        System.out.println("map:");
        Stream.of(list1, list2).map(str -> str.stream().map(String::toUpperCase)).forEach(System.out::println);
        //flatMap list合并
        System.out.println("flatMap:");
        Stream.of(list1, list2).flatMap(str -> str.stream().map(String::toUpperCase)).forEach(System.out::println);


        

                  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值