雪洗中关村

导航

java8 array、list操作 汇【1】),以及(Function,Consumer,Predicate,Supplier)解析

public static void main(String[] args){
        System.out.println("0、集合、数组操作:");
        // 使用lambda表达式 (每个订单加上12%的税)
        List<Integer> costBeforeTax = Arrays.asList(100, 200, 300, 400, 500);
        //集合->集合: 生成新的集合: 方法1:
        List<Integer> b1 = costBeforeTax.stream().map((cost) -> cost + 12*cost).collect(Collectors.toList());
        //集合->集合: 生成新的集合: 方法2:
        List<Integer> b2 = costBeforeTax.stream().map((cost) -> cost + 12*cost).collect(Collectors.toCollection(ArrayList<Integer>::new));


        int[] typePart = {0,1,4,5,7,8};//数组->数组:  生成新的数组
        String[] typePart2 = Arrays.stream(typePart).mapToObj((i) -> "A"+i).toArray(String[]::new);
//        Arrays.stream(typePart2).forEach(System.out::println);

        String[] typePart0 = {"2","3","5"};
        String temp = Streams.of(typePart0).filter(i -> i.equals("5")).findAny().orElse(null);
        System.out.println(temp);

        /* 来源: https://blog.csdn.net/turbo_zone/article/details/52557191?locationNum=2&fps=1
        //1、Function类 (compose方法和andThen方法一样接收一个另一个Function作为参数,但是顺序与andThen恰恰相反。 )
        Function类包含四种方法,其中一个抽象方法apply(),两个default方法andThen()和compose(),以及一个静态方法identity()。
        实例化Function的时候需要实现其中的apply()方法,apply方法接收一个模板类型作为输入参数,在andThen()和compose()方法里会自动调用apply()方法。
        andThen方法接收一个Function类的实例,通过andThen可以将任意多个Function的apply方法调用连接起来,在Java8里的源码如下:
        default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
                Objects.requireNonNull(after);
                return (T t) -> after.apply(apply(t));
        }
        */
        System.out.println("1、Function类:");
        function1.andThen(function2).apply("hello world");
        System.out.println("1.1、Function类举例1.1:");
        int incr = 20;  int myNumber = 10;
        modifyTheValue(myNumber, val-> val + incr);
        myNumber = 15;
        modifyTheValue(myNumber, val-> val * 10);
        modifyTheValue(myNumber, val-> val - 100);
        modifyTheValue(myNumber, val-> "somestring".length() + val - 100);


        System.out.println("2、Consumer类: ");
        /*
        //2、Consumer类

        */
        Foo f = new Foo();
        f.foo(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) {
                System.out.println(integer);
            }
        });
        //可简写成:  f.foo(integer -> System.out.println(integer));

        System.out.println("3、Supplier类: ");
        /*
         //3、Supplier类
        supplier的中文意思是提供者,跟Consumer类相反,Supplier类用于提供对象,它只有一个get方法,是一个抽象方法,需要编程者自定义想要返回的对象。
        */
        System.out.println("and: " + predicate1.and(predicate2).test(1));   //false
        System.out.println("or: " + predicate1.or(predicate2).test(1));     //true
        System.out.println("negate: " + predicate1.negate().test(1));       //true

        System.out.println("4、Supplier类 : ");
        /*
         //4、Supplier类

        */
        Supplier<Integer> supplier = new Supplier<Integer>() {
            @Override
            public Integer get() {
                return new Random().nextInt(100);
            }
        };
        //简化成lambda表达式: Supplier<Integer> supplier = () -> new Random().nextInt(100);
        int[] ints = new int[10];
        for(int i = 0; i < 10; i++) {
            ints[i] = supplier.get();
        }
        Arrays.stream(ints).forEach(System.out::println);



    }

 

posted on 2018-06-05 13:10  雪洗中关村  阅读(235)  评论(0)    收藏  举报