Java

java.util.function ํŒจํ‚ค์ง€

9taetae9 2023. 10. 6. 19:42
728x90

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() : ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ์—†๊ณ , ๋ฐ˜ํ™˜ ๊ฐ’๋งŒ ์žˆ์Œ

Gets a result.

Returns: a result

3) Consumer<T> (์†Œ๋น„์ž)

void accept(T t) : Supplier์™€ ๋ฐ˜๋Œ€๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋งŒ ์žˆ๊ณ , ๋ฐ˜ํ™˜๊ฐ’์ด ์—†์Œ

Performs this operation on the given argument.

Parameters:

t - the input argument

 

4) Function<T, R> (ํ•จ์ˆ˜)

R apply(T t) : ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜, ํ•˜๋‚˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐ›์•„์„œ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜

Applies this function to the given argument.

Parameters:

t - the function argument

Returns:

the function result

 

5) Predicate<T> (์กฐ๊ฑด์‹)

boolean test(T t) : ์กฐ๊ฑด์‹์„ ํ‘œํ˜„ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋จ. ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํ•˜๋‚˜, ๋ฐ˜ํ™˜ ํƒ€์ž…์€ boolean

**์›๋ž˜๋Š” Function<T,R>์ฒ˜๋Ÿผ Predicate<Integer, Boolean> ์ด๋ผ๊ณ  ์จ์•ผ ํ•˜์ง€๋งŒ ๋ฐ˜ํ™˜ํƒ€์ž…์ด ํ•ญ์ƒ Boolean์ด๊ธฐ ๋•Œ๋ฌธ์— ํ‘œ์‹œํ•˜์ง€ ์•Š๋Š”๋‹ค.

Evaluates this predicate on the given argument.

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

Evaluates this predicate on the given arguments.

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

 

728x90