java8常用的4个函数

@Test

public void testPredict() {

        Predicate predicate = (t) -> t>0;

        System.out.println(predicate.test(1));

        System.out.println(predicate.negate().test(1));

        Predicate predicate2 = Predicate.isEqual(1);

        System.out.println(predicate2.test(2));

        System.out.println(predicate.or(predicate2).test(2));

}

@Test

public void testConsumer() {

        Consumer consumer=new Consumer() {

                @Override

                  public void accept(String t) {

                            System.out.println(t);

                }

        };

        Consumer after=(String s)-> System.out.println(s+"  "  +1);

        consumer.accept("100");

        consumer.accept("asd");

        consumer.andThen(after).accept("11");;

}

@Test

public void testSupply () {

                Supplier supplier =new Supplier() {

                                @Override

                              public Integer get() {

                                        return 1;

                                }

                };            

                Supplier supplier1=()->1+3;

                System.out.println(supplier.get());

                System.out.println(supplier1.get());

        }

@Test

public void testFunction() {

            Function function1 =new Function() {

                        @Override

                          public String apply(Integer t) {

                                return String.valueOf(t);

                            }

                        };

            Function function2=(Integer t)->String.valueOf(t+2);

            System.out.println(function1.apply(12));

            System.out.println(function2.apply(1));

            System.out.println(Function.identity().apply("1"));

    }

你可能感兴趣的:(java8常用的4个函数)