1. public class CommonFunction {
    2. public static void main(String[] args) {
    3. //常用Java8内置接口
    4. //Consumer 消费类型 接口 demo 对1000进行消费 对这个参数进行操作
    5. CommonFunction.shop( 1000, new Consumer <Double>() {
    6. @Override
    7. public void accept(Double aDouble) {
    8. System.out.println( aDouble );
    9. }
    10. } );
    11. //Supiler get方式 Supplier<T>供给型接口 无 T 返回类型为T的对象,方法:T get();可用作工
    12. String code = CommonFunction.getCode( 4, () -> {
    13. return new Random( 10 ).nextInt();
    14. } );
    15. System.out.println(code);
    16. //Function 函数 操作类型为T的 返回为R
    17. Integer length = CommonFunction.functionGet( "张三", (str) -> str.length() );
    18. System.out.println(length);
    19. //输入参数 然后输入这个参数的判定条件
    20. boolean predicate = CommonFunction.predicate( "Lucy", (str) -> str.equals( "Lucy" ) );
    21. System.out.println(predicate);
    22. }
    23. //consumer函数
    24. public static void shop(double money, Consumer <Double> consumer) {
    25. consumer.accept( money );
    26. }
    27. //supplier函数
    28. public static String getCode(int num, Supplier<Integer>supplier){
    29. String str="";
    30. for (int i=0;i<num;i++){
    31. str+=supplier.get();
    32. }
    33. return str;
    34. }
    35. //Function函数
    36. public static Integer functionGet(String str,Function<String,Integer> function){
    37. return function.apply( str );
    38. }
    39. //Predicate函数 对某个参数是否符合返回boolean值
    40. public static boolean predicate(String str,Predicate<String>predicate){
    41. boolean test = predicate.test( str );
    42. return test;
    43. }
    44. }

    其他函数
    Java8常用函数 - 图1

    
    public class OtherFunction {
    
        //接受参数TT  方法 apply  对TT类型参数做二次运算
        public static BinaryOperator<Integer>binaryOperator=(x,y)->x+y;
        //T U R  对 T 和U  参数操作 返回类型为R
        public static BiFunction<String,Integer,String>biFunction=(x,y)->{
          return x+y;
        };
        //接受参数为T  对T类型的参数进行操作 返回类型也为T
        public  static UnaryOperator<String>unaryOperator=(x)->{
            return x.toString();
        };
        //接受参数为T和U   对T和U的参数进行操作 返回类型为void
        public static BiConsumer<String,Integer>biConsumer=(x,y)->{
            System.out.println(x+y);
        };
        //接收参数为Int 类型 对Int类型参数进行计算
        public static ToIntFunction<Integer>toIntFunction=(x)->{
          return   x+1;
        };
        //参数分别为int long double
        public static IntFunction<Integer>integerIntFunction=(x)->{
          return x;
        };
    
        public static void main(String[] args) {
            Integer apply1 = OtherFunction.binaryOperator.apply( 1, 2 );;
            System.out.println(apply1);
            String sex = OtherFunction.biFunction.apply( "sex", 6 );
            System.out.println(sex);
            String str = OtherFunction.unaryOperator.apply( "str" );
            System.out.println(str);
            OtherFunction.biConsumer.accept( "String",1 );
            int i = OtherFunction.toIntFunction.applyAsInt( 1 );
            System.out.println(i);
            Integer apply = OtherFunction.integerIntFunction.apply( 1 );
            System.out.println("apply"+apply);
        }
    }