Created
April 7, 2017 08:14
-
-
Save kodaitakahashi/85737570bff6ed7ccef1a75a6b471ae3 to your computer and use it in GitHub Desktop.
関数型インターフェースの理解を深めるため
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
import java.util.function.BiFunction; | |
import java.util.function.BinaryOperator; | |
import java.util.function.Consumer; | |
import java.util.function.Function; | |
import java.util.function.IntToDoubleFunction; | |
import java.util.function.Predicate; | |
import java.util.function.Supplier; | |
import java.util.function.ToDoubleFunction; | |
import java.util.function.ToIntFunction; | |
import java.util.function.ToLongFunction; | |
import java.util.function.UnaryOperator; | |
public class FuntionClass { | |
public static void main(String[] args) { | |
// Function | |
Function<String, Integer> fn1 = Integer::parseInt; | |
// Function<String, Integer> fn1 = s -> Integer.parseInt(s); | |
Integer apply = fn1.apply("111"); | |
System.out.println(apply); // 111 | |
/* | |
* Function <T, R> Tを受け取ってRを返す | |
* | |
* Function<String, Integer> Stringを受け取ってIntegerを返す | |
*/ | |
Function<String, String> wrapDoubleQuoute = s -> "\"" + s + "\""; | |
Function<String, String> wrapSingleQuote = s -> "'" + s + "'"; | |
Function<String, String> compose = wrapDoubleQuoute.compose(wrapSingleQuote); | |
String apply2 = compose.apply("hoge"); | |
System.out.println(apply2); // "'hoge'" | |
String apply3 = wrapSingleQuote.compose(wrapDoubleQuoute).apply("hoge"); | |
System.out.println(apply3); // '"hoge"' | |
/* | |
* comporse(<Function<T, R>, Function<T, R>) | |
* 引数で渡したFuntionを実行し、メソッドの呼び出し元のFuntionを実行する | |
* | |
* Functionを繋げたいときに使う | |
*/ | |
Function<String, String> andThen = wrapDoubleQuoute.andThen(wrapSingleQuote); | |
String apply4 = andThen.apply("hoge"); | |
System.out.println(apply4); | |
/* | |
* andThen<Function<? super R, e extends V>) | |
* メソッドの呼び出し元のFuntionを実行し、引数に渡したFunctionを実行する | |
* | |
* comporseの逆である | |
*/ | |
// UnaryOperator | |
UnaryOperator<String> uo = str -> "[" + str + "]"; | |
String apply6 = uo.apply("hoge"); | |
System.out.println(apply6); | |
/* | |
* Functionの拡張インターフェース Funtionの型引数が同じ型を返すもの | |
*/ | |
// Consumer | |
Consumer<String> consumer = string -> System.out.println("Consumer : " + string); | |
consumer.accept("hoge"); | |
/* | |
* Consumer<T> T型の値を引数に受け取って、戻り値なしの処理を実装する | |
*/ | |
// Supplier | |
Supplier<String> supplier = () -> "hoge"; | |
System.out.println(supplier.get()); | |
/* | |
* Supplier<T> T型の値を返す処理を実装 | |
* | |
*/ | |
// Predicate | |
Predicate<String> predicate = string -> string.isEmpty(); | |
System.out.println(predicate.test("")); | |
System.out.println(predicate.test("hoge")); | |
// Object#equalsを使う場合 | |
Predicate<String> predicate2 = Predicate.isEqual("hoge"); | |
System.out.println(predicate2.test("huga")); | |
System.out.println(predicate2.test("hoge")); | |
/* | |
* Predicate<T> T型の値を引き取って、booleanの値を返す処理を実装する | |
*/ | |
// BinaryOperator | |
BinaryOperator<Integer> maxBy = BinaryOperator.maxBy(Integer::compare); | |
Integer apply7 = maxBy.apply(20, 50); | |
System.out.println(apply7); | |
/* | |
* BiFunctionの拡張インターフェース T型の引数を2つ受け取ってT型の値を返す.apply(T, T)がある | |
*/ | |
// BiFuntion | |
BiFunction<String, Integer, String> biFn1 = (s, i) -> s + i; | |
String apply5 = biFn1.apply("hoge", 123); | |
System.out.println(apply5); | |
// ToIntFunction<T> | |
ToIntFunction<String> toIntFunction = String::length; | |
int applyAsInt = toIntFunction.applyAsInt("hogehogehoge"); | |
System.out.println(applyAsInt); | |
/* | |
* ToIntFunction<T> T型の値を受け取り、int型を返す処理を実装 | |
*/ | |
// ToLongFunction<T> | |
ToLongFunction<Long> random = num -> new Random(num).nextLong(); | |
long applyAsLong = random.applyAsLong(100L); | |
System.out.println(applyAsLong); | |
/* | |
* ToIntFuntion<T> T型の値を受け取り、Long型を返す処理を実装 | |
*/ | |
// ToDoubleFunction<T> | |
ToDoubleFunction<Long> random2 = num -> new Random(num).nextDouble(); | |
double applyAsDouble = random2.applyAsDouble(200L); | |
System.out.println(applyAsDouble); | |
/* | |
* ToDoubleFuntion<T> T型の値を受け取り、Double型を返す処理を実装 | |
*/ | |
// IntToLongFunction LongToIntFunction DoubleToLongFuntion | |
// LongToDoubleFuntion IntToDoubleFuntion DoubleToFuntion | |
IntToDoubleFunction intToDouble = num -> num; | |
double result = intToDouble.applyAsDouble(100); | |
System.out.println(result); | |
// IntToLongFuntion int型の値を受け取り、Double型を返す処理を実装 | |
// そのほかも同じ *To# *-> 渡すプリミティブ型 #->返すプリミティブ型 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment