java.util.function ํจํค์ง
java.util.function ํจํค์ง
- ์์ฃผ ์ฌ์ฉ๋๋ ๋ค์ํ ํจ์ํ ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณต
1) java.lang.Runnable // java.util.function ํจํค์ง๋ ์๋
void run() : ๋งค๊ฐ๋ณ์๋ ์๊ณ , ๋ฐํ๊ฐ๋ ์์
public class MyRunnable implements Runnable {
@Override
public void run() {
System.out.println("This is running in a thread.");
}
public static void main(String[] args) {
Thread thread = new Thread(new MyRunnable());
thread.start(); // This will invoke the run() method of MyRunnable in a new thread.
}
}
public static void main(String[] args) {
Thread thread = new Thread(() -> {
System.out.println("running in a thread");
});
thread.start(); //run()ํธ
}
2) Supplier<T> (๊ณต๊ธ์)
T get() : ๋งค๊ฐ๋ณ์๋ ์๊ณ , ๋ฐํ ๊ฐ๋ง ์์
Returns: a result
3) Consumer<T> (์๋น์)
void accept(T t) : Supplier์ ๋ฐ๋๋ก ๋งค๊ฐ๋ณ์๋ง ์๊ณ , ๋ฐํ๊ฐ์ด ์์
Parameters:
t - the input argument
4) Function<T, R> (ํจ์)
R apply(T t) : ์ผ๋ฐ์ ์ธ ํจ์, ํ๋์ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ์์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ
Parameters:
t - the function argument
Returns:
the function result
5) Predicate<T> (์กฐ๊ฑด์)
boolean test(T t) : ์กฐ๊ฑด์์ ํํํ๋๋ฐ ์ฌ์ฉ๋จ. ๋งค๊ฐ๋ณ์๋ ํ๋, ๋ฐํ ํ์ ์ boolean
**์๋๋ Function<T,R>์ฒ๋ผ Predicate<Integer, Boolean> ์ด๋ผ๊ณ ์จ์ผ ํ์ง๋ง ๋ฐํํ์ ์ด ํญ์ Boolean์ด๊ธฐ ๋๋ฌธ์ ํ์ํ์ง ์๋๋ค.
Parameters:
t - the input argument
Returns: true if the input argument matches the predicate, otherwise false
ex)
Predicate<String> isEmptyStr = s->s.length() == 0; (๋ฐํ ํ์ boolean)
String s = "";
if(isEmptyStr.test(s)) // if(s.length()==0)
System.out.println("This is an empty String.");
Supplier<Integer> f = () -> (int) (Math.random()*100)+1; //๋งค๊ฐ๋ณ์ ์์. ๋์ ๋ฐํ. (์ฃผ๊ธฐ๋ง ํ๋ ๊ณต๊ธ์)
Consumer<Integer> f = i -> System.out.println(i+", "); //๋งค๊ฐ ๋ณ์ ์๊ณ , ๋ฐํ ๊ฐ(x) (๋ฐ๊ธฐ๋ง ํ๋ ์๋น์)
Predicate<Integer> f = i -> i%2==0; // (์กฐ๊ฑด์)
Function<Integer, Integer> f = i -> i/10*10; //๋งค๊ฐ ๋ณ์ ์์. ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ.
๋งค๊ฐ๋ณ์๊ฐ 2๊ฐ์ธ ํจ์ํ ์ธํฐํ์ด์ค
1) BiConsumer<T,U>
void accept(T t , U u) : ๋๊ฐ์ ๋งค๊ฐ๋ณ์๋ง ์๊ณ , ๋ฐํ๊ฐ์ด ์์
Performs this operation on the given arguments.
Type Parameters:
T - the type of the first argument to the operation
U - the type of the second argument to the operation
BiConsumer<Integer, Integer> printSum = (a, b) -> System.out.println("Sum: " + (a + b));
BiConsumer<Integer, Integer> printProduct = (a, b) -> System.out.println("Product: " + (a * b));
BiConsumer<Integer, Integer> combinedConsumer = printSum.andThen(printProduct);
combinedConsumer.accept(5, 3);
output:
Sum: 8
Product: 15
2) BiPredicate<T, U>
boolean test(T t, U u) : ์กฐ๊ฑด์์ ํํํ๋๋ฐ ์ฌ์ฉ๋จ, ๋งค๊ฐ๋ณ์๋ ๋, ๋ฐํ๊ฐ์ boolean
Parameters:
t - the first input argument
u - the second input argument
Returns: true if the input arguments match the predicate, otherwise false
BiPredicate<Integer, Integer> biPredicate = (num1, num2) -> num1 > 10 && num2 < 5;
boolean result = biPredicate.test(15, 3); // return true
3) BiFunction<T, U, R>
R apply(T t, U u) : ๋ ๊ฐ์ ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ์์ ํ๋์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐํ
Applies this function to the given arguments.
Parameters:
t - the first function argument
u - the second function argument
Returns:the function result
BiFunction<Integer, Integer, String> biFunction = (num1, num2) -> "Result: " + (num1 + num2);
BiSupplier x
3๊ฐ ์ด์ (์ง์ ๊ตฌํ)
interface Tri Function<T,U,V,R> {
R apply (T t, U u, V v);
}
TriFunction<Integer, Integer, Integer, Integer> addThreeNumbers = (a, b, c) -> a + b + c;
int result = addThreeNumbers.apply(1, 2, 3); // 6
๋งค๊ฐ๋ณ์์ ํ์ ๊ณผ ๋ฐํํ์ ์ด ์ผ์นํ๋ ํจ์ํ ์ธํฐํ์ด์ค
1) UnaryOperator<T> ๋จํญ์ฐ์ฐ์
T apply(T t) : Fuction์ ์์, Function๊ณผ ๋ฌ๋ฆฌ ๋งค๊ฐ๋ณ์์ ๊ฒฐ๊ณผ์ ํ์ ์ด ๊ฐ๋ค.
public interface Function<T, R>{
R apply (T t);
...
}
public interface UnaryOperator<T> extends Function<T, T>{ //Function์ ์์๋ฐ์
static <T> UnaryOperator<T> identity{ //ํญ๋ฑํจ์
return t->t;
}
}
2) BinaryOperator<T> ์ดํญ์ฐ์ฐ์
T apply(T t, T t) : BiFunction์ ์์, BiFunction๊ณผ ๋ฌ๋ฆฌ ๋งค๊ฐ๋ณ์์ ๊ฒฐ๊ณผ์ ํ์ ์ด ๊ฐ๋ค.
public interface BiFunction<T, U, R> {
R apply(T t, U u);
// ...
}
public interface BinaryOperator<T> extends BiFunction<T, T, T> {
// ...
}
BinaryOperator<Integer> add = (a, b) -> a + b;
BinaryOperator<Integer> subtract = (a, b) -> a - b;
BinaryOperator<Integer> multiply = (a, b) -> a * b;
public static void main(String[] args) {
BinaryOperator<Integer> add = (a, b) -> a + b;
BinaryOperator<Integer> subtract = (a, b) -> a - b;
BinaryOperator<Integer> multiply = (a, b) -> a * b;
System.out.println(add.apply(5, 3)); // Outputs: 8
System.out.println(subtract.apply(5, 3)); // Outputs: 2
System.out.println(multiply.apply(5, 3)); // Outputs: 15
}
public static void main(String[] args) {
Supplier<Integer> s = ()->(int)(Math.random()*100)+1;
Consumer<Integer> c = i->System.out.print(i+ ", ");
Predicate<Integer> p = i -> i%2==0;
Function<Integer, Integer> f = i -> i/10*10; //์ผ์ ์๋ฆฌ ์ ๊ฑฐ
List<Integer> list = new ArrayList<>();
makeRandomList(s, list);
System.out.println(list);
printEvenNum(p, c, list);
List<Integer> newList = doSometing(f,list);
System.out.println(newList);
}
//Function<Integer, Integer> f = i -> i/10*10; //์ผ์ ์๋ฆฌ ์ ๊ฑฐ
static <T> List<T> doSometing(Function<T, T> f, List<T> list) {
List<T> newList = new ArrayList<T>(list.size());
for(T i : list) {
newList.add(f.apply(i));
}
return newList;
}
//Predicate<Integer> p = i -> i%2==0;
//Consumer<Integer> c = i->System.out.print(i+ ", ");
static <T> void printEvenNum(Predicate<T> p, Consumer<T> c, List<T> list) {
System.out.print("[");
for(T e: list) {
if(p.test(e)) { //์ง์์ธ์ง ๊ฒ์ฌ
c.accept(e);
}
}
System.out.print("]");
}
static <T> void makeRandomList(Supplier<T> s, List<T> list) {
for(int i=0; i<10; i++) {
list.add(s.get());
}
}
output :
[24, 7, 47, 19, 80, 43, 2, 89, 73, 53]
[24, 80, 2, ][20, 0, 40, 10, 80, 40, 0, 80, 70, 50]
์ฐธ๊ณ : https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
java.util.function (Java Platform SE 8 )
Interface Summary Interface Description BiConsumer Represents an operation that accepts two input arguments and returns no result. BiFunction Represents a function that accepts two arguments and produces a result. BinaryOperator Represents an operation u
docs.oracle.com