[Lambda ] 기본형 함수형 인터페이스 : {Int, Double, Long} {Consumer, Supplier, Predicate,UnaryOperator,BinaryOperator}
IntConsumer : public void accept(int value)
IntPredicate : public boolean test(int value)
IntUnaryOperator : int applyAsInt(int operand)
IntBinaryOperator : public int applyAsInt(int left, int right)
DoubleConsumer : public void accept(double value)
DoublePredicate : public boolean test(double value)
DoubleUnaryOperator : double applyAsInt(double operand)
DoubleBinaryOperator : public double applyAsInt(double left, double right)
LongConsumer : public void accept(long value)
LongPredicate : public boolean test(long value)
LongUnaryOperator : long applyAsInt(long operand)
LongBinaryOperator : public long applyAsInt(long left, long right)
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);
};
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);
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));
}
}
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));
}
}
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);
}
}
[Lambda] 기본형 함수형 인터페이스 : ToIntFunction<T>, ToIntBiFunction<T, U> 등 (0) | 2020.06.21 |
---|---|
[Lambda] 기본형 함수형 인터페이스 : AToBFunction (0) | 2020.06.21 |
[Collection, Map, Lambda] replaceAll : 함수형 인터페이스를 파라미터로 가지는 컬렉션 메소드 (0) | 2020.06.19 |
[Collection, Map, Lambda] merge : 함수형 인터페이스를 파라미터로 가지는 컬렉션 메소드 (0) | 2020.06.19 |
[Collection, Map, Lambda] computeIfPresent : 함수형 인터페이스를 파라미터로 가지는 컬렉션 메소드 (0) | 2020.06.19 |
댓글 영역