상세 컨텐츠

본문 제목

[Lambda ] 기본형 함수형 인터페이스 : {Int, Double, Long} {Consumer, Supplier, Predicate,UnaryOperator,BinaryOperator}

Java

by kwanghyup 2020. 6. 20. 18:03

본문

1. int Type 

  • IntConsumer :   public void accept(int value)

  • intSupplier : public int getAsInt()
  • IntPredicate : public boolean test(int value)

  • IntUnaryOperator : int applyAsInt(int operand) 

  • IntBinaryOperator : public int applyAsInt(int left, int right)

2. double Type

  • DoubleConsumer :   public void accept(double value) 

  • DoubleSupplier : public double getAsDouble()
  • DoublePredicate : public boolean test(double value)

  • DoubleUnaryOperator : double applyAsInt(double operand) 

  • DoubleBinaryOperator : public double applyAsInt(double left, double right)

  • intSupplier : public int getAsInt()

 

3. long Type

  • LongConsumer :   public void accept(long value) 

  • LongSupplier : public long getAslong()
  • LongPredicate : public boolean test(long value)

  • LongUnaryOperator : long applyAsInt(long operand) 

  • LongBinaryOperator : public long applyAsInt(long left, long right)

 

IntConsumer :   public void accept(int value)

import java.util.function.IntConsumer;

public class Example {
    public static void main(String[] args) {
        IntConsumer consumer = new IntConsumer() {
            @Override
            public void accept(int value) {
                int random = (int) (Math.random()*value+1);
                System.out.println(random);
            }
        };

        intConsumerTest(consumer,12);
    }

    public static void intConsumerTest(IntConsumer consumer, int value){
        consumer.accept(value);
    }

}
// 람다
IntConsumer consumer = value -> {
    int random = (int) (Math.random()*value+1);
    System.out.println(random);
};

 

IntSupplier : pubilc int getAsInt() 

import java.util.Arrays;
import java.util.function.IntSupplier;

public class Example {

    public static void main(String[] args) {
        IntSupplier supplier = new IntSupplier() {
            @Override
            public int getAsInt() {
                return (int) (Math.random()*10+1); // 1~10 정수
            }
        };

        int[] arr = new int[10];
        arr = test(supplier,arr);
        System.out.println(Arrays.toString(arr));

    }
        public static int[] test(IntSupplier supplier, int[] arr){
            for (int i = 0; i < arr.length; i++) {
                arr[i] = supplier.getAsInt();
            }
            return arr;
        }
}
// 람다 
IntSupplier supplier = () ->  (int) (Math.random()*10+1); 

 

IntPredicate : public boolean test(int value)

import java.util.function.*;

public class Example {

    public static void main(String[] args) {
        IntPredicate intPredicate = new IntPredicate() {
            @Override
            public boolean test(int value) {
                return value>100;
            }
        };

        IntPredicate lambda = value -> value > 100;

        System.out.println(intPredicate.test(102));
        System.out.println(intPredicate.test(99));
        System.out.println(lambda.test(112));
        System.out.println(lambda.test(98));
    }
}

 

IntUnaryOperator : int applyAsInt(int operand) 

import java.util.function.*;

public class Example {

    public static void main(String[] args) {
        IntUnaryOperator intUnaryOperator = new IntUnaryOperator() {
            @Override
            public int applyAsInt(int operand) {
                return operand*operand;
            }
        };

        IntUnaryOperator lambda = operand -> operand*operand;

        System.out.println(intUnaryOperator.applyAsInt(11));
        System.out.println(lambda.applyAsInt(12));
    }
}

 

IntBinaryOperator : public int applyAsInt(int left, int right)

import java.util.function.*;

public class Example {

    public static void main(String[] args) {
        IntBinaryOperator intBinaryOperator = new IntBinaryOperator() {
            @Override
            public int applyAsInt(int left, int right) {
                return left*right;
            }
        };

        IntBinaryOperator lambda = ((left, right) -> right*left);

        int r = intBinaryOperator.applyAsInt(10,4);
        System.out.println(r);

        r = lambda.applyAsInt(8,4);
        System.out.println(r);
    }
}

 

 

관련글 더보기

댓글 영역