JAVA8新特性之函数式接口(二)

DoubleSupplier:
代表一个double值结构的提供方

   public static void main(String[] args) {
         System.out.println("------DoubleSupplier------");
        DoubleSupplier doubleSupplier=()->8.0;
        double qe = 8.0;
        double qt = 9.7;
        DoubleSupplier doubleSupplier1=()->qe*qt;
        System.out.println("DoubleSupplier>>>"+doubleSupplier.getAsDouble());
        System.out.println("doubleSupplier1>>>"+doubleSupplier1.getAsDouble());
        System.out.println("------DoubleSupplier------");
    }

输出内容为:

------DoubleSupplier------
DoubleSupplier>>>8.0
doubleSupplier1>>>77.6
------DoubleSupplier------

DoubleToIntFunction:
接受一个double类型输入,返回一个int类型结果。

   public static void main(String[] args) {
        System.out.println("------DoubleToIntFunction------");
        int x1= 8;
        DoubleToIntFunction doubleToIntFunction = (qy)->{
            return (int)(qy*x1);
        };
        System.out.println("doubleToIntFunction>>>"+doubleToIntFunction.applyAsInt(8.95));
        System.out.println("------DoubleToIntFunction------");
    }

输出内容为:

------DoubleToIntFunction------
doubleToIntFunction>>>71
------DoubleToIntFunction------

DoubleToLongFunction:
接受一个double类型输入,返回一个long类型结果

   public static void main(String[] args) {
       System.out.println("------DoubleToLongFunction------");
        long x2= 8;
        DoubleToLongFunction doubleToLongFunction = (qy)->{
            return (long) qy*x2;
        };
        System.out.println("DoubleToLongFunction>>>"+doubleToLongFunction.applyAsLong(9.5858));
        System.out.println("------DoubleToLongFunction------");
    }

输出内容为:

------DoubleToLongFunction------
DoubleToLongFunction>>>72
------DoubleToLongFunction------

DoubleUnaryOperator:
接受一个参数同为类型double,返回值类型也为double 。注意,这种类似的andThen方法,是先将andThen里面的参数做applyAsDouble操作,然后把结果在做为参数传给最外面的那个doubleUnaryOperator做applyAsDouble方法。

  public static void main(String[] args) {
       System.out.println("------DoubleUnaryOperator------");
       DoubleUnaryOperator doubleUnaryOperator = (qu)->qu*2;
       DoubleUnaryOperator doubleUnaryOperator1 = (qu)->qu+2;
       System.out.println("DoubleUnaryOperator applyAsLong >>>"+doubleUnaryOperator.applyAsDouble(9.00));
       System.out.println("DoubleUnaryOperator compose >>>"+doubleUnaryOperator.compose(doubleUnaryOperator1).applyAsDouble(18.0));
       System.out.println("DoubleUnaryOperator andThen >>>"+doubleUnaryOperator.andThen(doubleUnaryOperator).applyAsDouble(16.0));
       System.out.println("------DoubleUnaryOperator------");
   }

输出内容为:

------DoubleUnaryOperator------
DoubleUnaryOperator applyAsLong >>>18.0
DoubleUnaryOperator compose >>>40.0
DoubleUnaryOperator andThen >>>64.0
------DoubleUnaryOperator------

Function:
接受一个输入参数,返回一个结果。

  public static void main(String[] args) {
              System.out.println("------Function------");
       Function<String,String> stringFunction = qi->qi+"   Hello Word";
       Function<String,String> stringFunction1 = qo->qo+"   How Are You?";
       System.out.println("Function >>>>>>"+stringFunction.apply("JACK"));
       System.out.println("Function andThen>>>>>>"+stringFunction.andThen(stringFunction1).apply("Tom"));
       System.out.println("Function compose>>>>>>"+stringFunction.compose(stringFunction1).apply("Tony"));
       System.out.println("------Function------");
   }

输出内容为:

------Function------
Function >>>>>>JACK   Hello Word
Function andThen>>>>>>Tom   Hello Word   How Are You?
Function compose>>>>>>Tony   How Are You?   Hello Word
------Function------

IntBinaryOperator:
接受两个参数同为类型int,返回值类型也为int 。

   public static void main(String[] args) {
              System.out.println("------IntBinaryOperator------");
        IntBinaryOperator binaryOperator1=(qp,qa)->{
            if (qp>qa){
                return qp-qa;
            }else {
                return qa-qp;
            }
        };
        System.out.println("IntBinaryOperator >>>>>>"+binaryOperator1.applyAsInt(8,5));
        System.out.println("IntBinaryOperator >>>>>>"+binaryOperator1.applyAsInt(1,5));
        System.out.println("------IntBinaryOperator------");
    }

输出内容为:

------IntBinaryOperator------
IntBinaryOperator >>>>>>3
IntBinaryOperator >>>>>>4
------IntBinaryOperator------

IntConsumer:
接受一个int类型的输入参数,无返回值 。

  public static void main(String[] args) {
     System.out.println("------IntConsumer------");
       IntConsumer intConsumer = qs->{
           System.out.println(qs*100000);
       };
       IntConsumer intConsumer1 = qd->{
           System.out.println(">>>>>"+qd*15);
       };
       intConsumer.accept(789);
       intConsumer1.andThen(intConsumer).accept(8);
       System.out.println("------IntConsumer------");
   }

输出内容为:

------IntConsumer------
78900000
>>>>>120
800000
------IntConsumer------

** IntFunction:**
接受一个int类型输入参数,返回一个结果(类型不限)

  public static void main(String[] args) {
      System.out.println("------IntFunction------");
       IntFunction<Double> intFunction = qf->{
           return x*0.88;
       };
       System.out.println("IntFunction >>>>>"+intFunction.apply(5));
       System.out.println("------IntFunction------");
   }

输出内容为:

------IntFunction------
IntFunction >>>>>1.76
------IntFunction------

IntPredicate:
接受一个int输入参数,返回一个布尔值的结果。

public static void main(String[] args) {
    System.out.println("------IntPredicate------");
     IntPredicate intPredicate = qg->{
         return qg>100;
     };
     IntPredicate intPredicate1 = qh->{
         return qh<100;
     };
     System.out.println("IntPredicate Test>>>>"+intPredicate.test(99));
     System.out.println("IntPredicate Test>>>>"+intPredicate.test(150));
     System.out.println("IntPredicate negate>>>>"+intPredicate.negate().test(150));
     System.out.println("IntPredicate and>>>>"+intPredicate.and(intPredicate1).test(150));
     System.out.println("IntPredicate or>>>>"+intPredicate.or(intPredicate1).test(150));
     System.out.println("------IntPredicate------");
 }

输出内容为:

------IntPredicate------
IntPredicate Test>>>>false
IntPredicate Test>>>>true
IntPredicate negate>>>>false
IntPredicate and>>>>false
IntPredicate or>>>>true
------IntPredicate------

IntSupplier:
无参数,返回一个int类型结果

  public static void main(String[] args) {
       System.out.println("------IntSupplier------");
       IntSupplier intSupplier = ()->{
               double str = 11.0;
               return (int)str;
       };
       System.out.println("IntSupplier >>>>"+intSupplier.getAsInt());
       System.out.println("------IntSupplier------");
   }

输出内容为:

------IntSupplier------
IntSupplier >>>>11
------IntSupplier------

IntToDoubleFunction:
接受一个int类型输入,返回一个double类型结果 。

  public static void main(String[] args) {
       System.out.println("------IntToDoubleFunction------");
       IntToDoubleFunction intToDoubleFunction = qj->{
           return qj*0.05;
       };
       System.out.println("intToDoubleFunction >>>>"+intToDoubleFunction.applyAsDouble(19));
       System.out.println("------IntToDoubleFunction------");
   }

输出内容为:

------IntToDoubleFunction------
intToDoubleFunction >>>>0.9500000000000001
------IntToDoubleFunction------

IntToLongFunction:
IntToLongFunction接受一个int类型输入,返回一个long类型结果。

   public static void main(String[] args) {
       System.out.println("------IntToLongFunction------");
        IntToLongFunction intToLongFunction = qk->{
            long ll = (long)2.501 ;
            return qk*ll;
        };
        System.out.println("IntToLongFunction >>>>"+intToLongFunction.applyAsLong(182));
        System.out.println("------IntToLongFunction------");
    }

输出内容为:

------IntToLongFunction------
IntToLongFunction >>>>364
------IntToLongFunction------

IntUnaryOperator:
接受一个参数同为类型int,返回值类型也为int 。

public static void main(String[] args) {
    System.out.println("------IntUnaryOperator------");
     IntUnaryOperator intUnaryOperator = ql->{
         return ql *100;
     };
     IntUnaryOperator intUnaryOperator1 = qz->{
         return qz *500;
     };
     System.out.println("IntToLongFunction >>>>"+intUnaryOperator.applyAsInt(52));
     System.out.println("IntToLongFunction >>>>"+intUnaryOperator1.applyAsInt(52));
     System.out.println("IntToLongFunction andThen >>>>"+intUnaryOperator.andThen(intUnaryOperator1).applyAsInt(52));
     System.out.println("IntToLongFunction compose >>>>"+intUnaryOperator.compose(intUnaryOperator1).applyAsInt(52));
     System.out.println("------IntUnaryOperator------");

 }

输出内容为:

------IntUnaryOperator------
IntToLongFunction >>>>5200
IntToLongFunction >>>>26000
IntToLongFunction andThen >>>>2600000
IntToLongFunction compose >>>>2600000
------IntUnaryOperator------

LongBinaryOperator:
接受两个参数同为类型long,返回值类型也为long。

   public static void main(String[] args) {
        System.out.println("------LongBinaryOperator------");
        LongBinaryOperator longBinaryOperator = (qx,qc)->{
            if (qx>qc){
               return qx - qc;
            }else{
               return qx*qc;
            }
        };
        System.out.println("LongBinaryOperator >>>>"+longBinaryOperator.applyAsLong(18,9));
        System.out.println("LongBinaryOperator >>>>"+longBinaryOperator.applyAsLong(80,90));
        System.out.println("------LongBinaryOperator------");

    }

输出内容为:

------LongBinaryOperator------
LongBinaryOperator >>>>9
LongBinaryOperator >>>>7200
------LongBinaryOperator------

LongConsumer:
接受一个long类型的输入参数,无返回值。

   public static void main(String[] args) {
        System.out.println("------LongConsumer------");
        LongConsumer longConsumer = (qv)-> System.out.println(">>>>>>>"+qv);
        LongConsumer longConsumer1 = (qb)-> System.out.println("<<<<<<"+qb);
        longConsumer.accept(8);
        longConsumer1.andThen(longConsumer).accept(999);
        System.out.println("------LongConsumer------");
    }

输出内容为:

------LongConsumer------
>>>>>>>8
<<<<<<999
>>>>>>>999
------LongConsumer------

LongFunction:
接受一个long类型输入参数,返回一个结果。

   public static void main(String[] args) {
        System.out.println("------LongFunction------");
        LongFunction longFunction = qn->{
            return String.valueOf(qn);
        };
        System.out.println("LongFunction >>>>"+ longFunction.apply(12333333));
        System.out.println("LongFunction type>>>>"+ (longFunction.apply(12333333)).getClass().toString());
        System.out.println("------LongFunction------");
    }

输出内容为:

------LongFunction------
LongFunction >>>>12333333
LongFunction type>>>>class java.lang.String
------LongFunction------

LongPredicate:
R接受一个long输入参数,返回一个布尔值类型结果。

public static void main(String[] args) {
     System.out.println("------LongPredicate------");
     LongPredicate longPredicate = qm->{
         if(qm>100){
             return true;
         }else{
             return false;
         }
     };
     LongPredicate longPredicate1 = wq->{
         if(wq<100){
             return true;
         }else{
             return false;
         }
     };
     System.out.println("LongPredicate >>>>"+ longPredicate.test(10000000));
     System.out.println("LongPredicate >>>>"+ longPredicate.test(10));
     System.out.println("LongPredicate >>>>"+ longPredicate.negate().test(10));
     System.out.println("LongPredicate and >>>>"+ longPredicate.and(longPredicate).test(100));
     System.out.println("LongPredicate or >>>>"+ longPredicate.or(longPredicate).test(100));
     System.out.println("------LongPredicate------");
 }

输出内容为:

------LongPredicate------
LongPredicate >>>>true
LongPredicate >>>>false
LongPredicate >>>>true
LongPredicate and >>>>false
LongPredicate or >>>>false
------LongPredicate------

LongSupplier:
无参数,返回一个结果long类型的值。

public static void main(String[] args) {
       System.out.println("------LongSupplier------");
     LongSupplier longSupplier = ()->{
         return Long.valueOf("111111");
     };
     System.out.println("LongSupplier >>>>"+ longSupplier.getAsLong());
     System.out.println("------LongSupplier------");
 }

输出内容为:

------LongSupplier------
LongSupplier >>>>111111
------LongSupplier------

LongToDoubleFunction:
接受一个long类型输入,返回一个double类型结果。

 public static void main(String[] args) {
      System.out.println("------LongToDoubleFunction------");
      LongToDoubleFunction longToDoubleFunction =  (ww)->{
          return Double.valueOf(ww);
      };
      System.out.println("longToDoubleFunction >>>>"+ longToDoubleFunction.applyAsDouble(1000000));
      System.out.println("------LongToDoubleFunction------");
  }

输出内容为:

------LongToDoubleFunction------
longToDoubleFunction >>>>1000000.0
------LongToDoubleFunction------

LongToIntFunction:
接受一个long类型输入,返回一个int类型结果。

 public static void main(String[] args) {
    System.out.println("------LongToIntFunction------");
      LongToIntFunction longToIntFunction =  (wr)->{
          return (int)wr;
      };
      System.out.println("longToDoubleFunction >>>>"+ longToIntFunction.applyAsInt(9999999));
      System.out.println("------LongToIntFunction------");
  }

输出内容为:

------LongToIntFunction------
longToDoubleFunction >>>>9999999
------LongToIntFunction------
发布了62 篇原创文章 · 获赞 8 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/system_obj/article/details/88959799