Exemple #1
0
  public static void main(String... args) {

    int c = 2;
    IntBinaryOperator i = (a, b) -> a + c;
    out.println(i.applyAsInt(1, 10));
    c = 3;
  }
  public static void main(String[] args) {

    String s = "abc";
    IntSupplier supplier = s::length;
    System.out.println(supplier.getAsInt());

    String s2 = "ぶーさん";
    IntSupplier supplier2 = () -> s2.length();
    System.out.println(supplier2.getAsInt());

    String s3 = "ブタ過ぎる";
    IntSupplier supplier3 =
        new IntSupplier() {

          @Override
          public int getAsInt() {
            return s3.length();
          }
        };
    System.out.println(supplier3.getAsInt());

    // ---------------------------------------------------------

    Consumer<String> c = System.out::println;
    c.accept("pig!!");

    Consumer<String> c2 = (String cs) -> System.out.println(cs);
    c2.accept("buta");

    Consumer<String> c3 =
        new Consumer<String>() {

          @Override
          public void accept(String t) {
            System.out.println(t);
          }
        };
    c3.accept("ぶうさーん!");

    // ---------------------------------------------------------
    IntBinaryOperator op = Integer::sum;
    System.out.println(op.applyAsInt(100, 25));

    IntBinaryOperator op2 = (int n1, int n2) -> Integer.sum(n1, n2);
    System.out.println(op2.applyAsInt(70, 50));
  }
 @Override
 public int reduce(int identity, IntBinaryOperator op) {
   return performOperation(
       TerminalFunctions.reduceFunction(identity, op),
       true,
       (i1, i2) -> op.applyAsInt(i1, i2),
       null);
 }
 @Override
 public OptionalInt reduce(IntBinaryOperator op) {
   Integer result =
       performOperation(
           TerminalFunctions.reduceFunction(op),
           true,
           (i1, i2) -> {
             if (i1 != null) {
               if (i2 != null) {
                 return op.applyAsInt(i1, i2);
               }
               return i1;
             }
             return i2;
           },
           null);
   if (result == null) {
     return OptionalInt.empty();
   } else {
     return OptionalInt.of(result);
   }
 }