関数インタフェース(Functional Interface)-セクション2
13773 ワード
今回のプレゼンテーションでは、Java SEが提供する組み込みライブラリjava.util.functionの関数インタフェースについて簡単に理解するjunit 5テストコードを作成します.
コードシンボルリンクのテスト
Interface Function<T,R>
T - the type of the input to the function
R - the type of the result of the function
T型データを入力し、R型データの関数型インタフェースを出力します.
apply(T t)
Applies this function to the given argument.
-->Tタイプのtを関数の入力値として関数を実行
compose(Function before)
Returns a composed function that first applies the before function to its input, and then applies this function to the result.
-->before関数を入力値として実行し、入力値として受信して実行する合成関数を返します.
andThen(Function after)
Returns a composed function that first applies this function to its input, and then applies the after function to the result.
-->関数のinputを受信して実行する合成関数を返し、結果値をafter関数のinputとして実行します.
T - the type of the first argument to the function
U - the type of the second argument to the function
R - the type of the result of the function
-->TとU型の関数インタフェースで、入力データを受信してR型のデータを出力する
apply(T t, U u)
Applies this function to the given arguments.
-->Functionのapply()と似ていますが、2つのパラメータがあります.
Returns a composed function that first applies this function to its input, and then applies the after function to the result.
-->FunctionのandThen()と同様の方法
T - the type of the input to the operation
Inputを使用してTタイプのデータを受信(リターンなし)
Performs this operation on the given argument.
->Tタイプのt実行オペレータ
andThen(Consumer after)
Returns a composed Consumer that performs, in sequence, this operation followed by the after operation.
->consumer操作が完了したら、consumer操作を順番に実行します
T - the type of results supplied by this supplier
->Tタイプは結果を返します
get()
Gets a result.
->結果を返す
T - the type of the input to the predicate
-->Tタイプの入力です.
Represents a predicate (boolean-valued function) of one argument.
-->パラメータを表すboolean-value.
test(T t)
Evaluates this predicate on the given argument.
-->述語を実行して、指定したパラメータのboolean-valueを返します.
Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.
-->は「other」述語戻り値と現在の述語戻り値と呼ばれる短いループ論理OR(すなわち|)演算結果を返す
Returns a predicate that represents the logical negation of this predicate.
-->述語の逆の結果(すなわち!)戻る
Returns a predicate that tests if two arguments are equal according to Objects.equals(Object, Object).
-->ん…コードで見るとずっと便利です.
Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.
-->は「other」述語戻り値と現在の述語戻り値と呼ばれる短いループ論理OR(すなわち|)演算結果を返す
java.util.functionが提供する関数型インタフェースでは、5つの関数型インタフェースが表示されます.このほかにも、けいかくを直接勉強しましょう.
注:Java、Java SE API
コードシンボルリンクのテスト
Interface Function<T,R>
T - the type of the input to the function
R - the type of the result of the function
T型データを入力し、R型データの関数型インタフェースを出力します.
@Test
@DisplayName("Function<T,R> 인터페이스 테스트")
void testFunctionInterface(){
// Integer 를 입력값으로 함수를 수행 후 Double return
Function<Integer, Double> func = (i) -> {
return Double.valueOf(i);
};
assertThat(func.apply(10)).isEqualTo(Double.valueOf(10));
}
java.util.functionのFunctionインタフェースの主な方法.apply(T t)
Applies this function to the given argument.
-->Tタイプのtを関数の入力値として関数を実行
compose(Function before)
Returns a composed function that first applies the before function to its input, and then applies this function to the result.
-->before関数を入力値として実行し、入力値として受信して実行する合成関数を返します.
andThen(Function after)
Returns a composed function that first applies this function to its input, and then applies the after function to the result.
-->関数のinputを受信して実行する合成関数を返し、結果値をafter関数のinputとして実行します.
@Test
@DisplayName("Function<T,R> 의 compose(), andThen() 메서드 테스트")
void funcIntCompose(){
Function<Integer, Integer> plus10 = (i) -> i+10;
Function<Integer, Integer> multiplyBy2 = (i) -> i * 2;
assertThat(plus10.apply(2)).isEqualTo(12);
assertThat(multiplyBy2.apply(2)).isEqualTo(4);
// input 받은 2를 multiplyBy2 의 입력값으로 multiplyBy2 함수를 수행한 후, 그 결과값을 plus10의 입력값으로 하여 함수를 수행
assertThat(plus10.compose(multiplyBy2).apply(2)).isEqualTo(14);
// input 받은 2를 수행한 결과값을 multiplyBy2 함수의 input 으로 하여 multiplyBy2 수행
assertThat(plus10.andThen(multiplyBy2).apply(2)).isEqualTo(24);
}
Interface BiFunction<T, U, R> T - the type of the first argument to the function
U - the type of the second argument to the function
R - the type of the result of the function
-->TとU型の関数インタフェースで、入力データを受信してR型のデータを出力する
apply(T t, U u)
Applies this function to the given arguments.
-->Function
@Test
@DisplayName("BiFunction<T, U, R> 의 apply()")
void biFunctionApply(){
BiFunction<String, String, String> stringConcat = (s1, s2) -> s1+s2;
assertThat(stringConcat.apply("hello", " world")).isEqualTo("hello world");
}
andThen(Function after)Returns a composed function that first applies this function to its input, and then applies the after function to the result.
-->Function
@Test
@DisplayName("BiFunction<T, U, R> 의 andThen()")
void biFunctionAndThe(){
BiFunction<String, String, String> stringConcat = (s1, s2) -> s1+s2;
Function<String, String> stringConcat2 = (s) -> s + " !!!";
assertThat(stringConcat.andThen(stringConcat2).apply("hello", " world")).isEqualTo("hello world !!!");
}
Interface Consumer<T> T - the type of the input to the operation
Inputを使用してTタイプのデータを受信(リターンなし)
@Test
@DisplayName("Consumer<T> 인터페이스 테스트")
void consumerTest(){
Consumer<String> HelloToName = (name) -> System.out.println("Hello " + name);
HelloToName.accept("jaden");
}
------- console
Hello jaden
accept(T t)Performs this operation on the given argument.
->Tタイプのt実行オペレータ
andThen(Consumer after)
Returns a composed Consumer that performs, in sequence, this operation followed by the after operation.
->consumer操作が完了したら、consumer操作を順番に実行します
@Test
@DisplayName("Consumer<T> 의 andThen() 테스트")
void consumerAndThen() {
Consumer<String> HelloToName = (name) -> System.out.println("Hello " + name);
Consumer<String> ByeToName = (name) -> System.out.println("Bye " + name);
HelloToName.andThen(ByeToName).accept("jaden");
}
------- console
Hello jaden
Bye jaden
Interface Supplier <T> T - the type of results supplied by this supplier
->Tタイプは結果を返します
get()
Gets a result.
->結果を返す
@Test
@DisplayName("Supplier<T> 의 get()")
void supplierTest(){
String expectedToSupplied = "supply this!";
Supplier<String> supplier = () -> expectedToSupplied;
assertThat(supplier.get()).isEqualTo(expectedToSupplied);
}
Interface Predicate<T> T - the type of the input to the predicate
-->Tタイプの入力です.
Represents a predicate (boolean-valued function) of one argument.
-->パラメータを表すboolean-value.
test(T t)
Evaluates this predicate on the given argument.
-->述語を実行して、指定したパラメータのboolean-valueを返します.
@Test
@DisplayName("Predicate<T> 의 test()")
void predicateTest(){
Predicate<String> startWithJaden = (s) -> s.startsWith("Jaden");
assertThat(startWithJaden.test("Jaden")).isTrue();
assertThat(startWithJaden.test("SomethingElse")).isFalse();
}
or(Predicate other)Returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.
-->は「other」述語戻り値と現在の述語戻り値と呼ばれる短いループ論理OR(すなわち|)演算結果を返す
@Test
@DisplayName("Predicate<T> 의 or(Predicate<? super T> other)")
void predicateOr(){
Predicate<Integer> isOdd = (i) -> i % 2 == 1;
Predicate<Integer> isIntegerThree = (i) -> i.equals(Integer.valueOf(3));
assertThat(isOdd.test(1)).isTrue();
assertThat(isOdd.test(2)).isFalse();
assertThat(isIntegerThree.test(3)).isTrue();
assertThat(isIntegerThree.test(1)).isFalse();
assertThat(isOdd.or(isIntegerThree).test(1)).isTrue();
assertThat(isOdd.or(isIntegerThree).test(2)).isFalse();
assertThat(isOdd.or(isIntegerThree).test(3)).isTrue();
}
negate()Returns a predicate that represents the logical negation of this predicate.
-->述語の逆の結果(すなわち!)戻る
@Test
@DisplayName("Predicate<T> 의 negate()")
void predicateNegate(){
Predicate<Integer> isIntegerOne = (i) -> i.equals(Integer.valueOf(1));
assertThat(isIntegerOne.negate().test(1)).isFalse();
}
isEqual(Object targetRef)Returns a predicate that tests if two arguments are equal according to Objects.equals(Object, Object).
-->ん…コードで見るとずっと便利です.
@Test
@DisplayName("Predicate<T> 의 isEqual()")
void predicateIsEqual(){
Predicate<String> predicate = Predicate.isEqual("test1");
assertThat(predicate.test("test1")).isTrue();
assertThat(predicate.test("test2")).isFalse();
}
and(Predicate other)Returns a composed predicate that represents a short-circuiting logical AND of this predicate and another.
-->は「other」述語戻り値と現在の述語戻り値と呼ばれる短いループ論理OR(すなわち|)演算結果を返す
java.util.functionが提供する関数型インタフェースでは、5つの関数型インタフェースが表示されます.このほかにも、けいかくを直接勉強しましょう.
注:Java、Java SE API
Reference
この問題について(関数インタフェース(Functional Interface)-セクション2), 我々は、より多くの情報をここで見つけました https://velog.io/@jaden_94/함수형-인터페이스-Functional-Interface-part2テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol