é¢æ°åã¤ã³ã¿ã¼ãã§ã¤ã¹ãå¤ãããçç±ã調ã¹ã¦ã¿ã
Java8ã«ã¤ãã¦èª¿ã¹ãã¨ãæ¨æºã§ç¨æããã¦ããé¢æ°åã¤ã³ã¿ã¼ãã§ã¤ã¹é¢æ°ã®å¤ãã«ã³ã£ããããã
SupplierãConsumerãPredicateãFunctionãUnaryOperatorãBinaryOperatorãetc..
å
¨é¨ã§40以ä¸ã¯ããã
æ£ç´ã¯ããã¯ããå
¨é¨è¦ããã®ãã¨ãããªãæ鬱ã ã£ãããããã¾ãã«ä¸è¨ã®4ã¤ã®ä½¿ãæ¹ãç解ã§ããã°ç¹ã«åé¡ãªãã¨æã£ã¦ããã(ä»ã®ã¯ããã®æ´¾çã«ãããªããã)
ã» Supplier
ã» Consumer
ã» Predicate
ã» Function
ãã ããªãã§ãããªã«å¤ããã ããã¨æã人ãããã¨æãã
å°ãªãã¦ãèªåã¯æã£ãã
ä¾ãã°ä¸è¨ã®ã³ã¼ããè¦ã¦ã¿ãã
public static void main(String[] args) { // æåæ°ãè¿å´ããFunctionãå®ç¾© Function<String, Integer> func = x -> x.length(); // åºå System.out.println(func.apply("sample")); // 6 }
åãå 容ãToIntFunctionã§ãåç¾ã§ããã
public static void main(String[] args) { // æåæ°ãè¿å´ããFunctionãå®ç¾© ToIntFunction<String> func2 = x -> x.length(); // åºå System.out.println(func2.applyAsInt("sample")); // 6 }
åæ§ã«BiFunction,ToIntBiFunctionã§ãåããã¨ãè¨ããã
public static void main(String[] args) { // æåæ°ã足ãã¦è¿å´ããFunctionãå®ç¾© BiFunction<String, String, Integer> func3 = (x, y) -> (x + y).length(); ToIntBiFunction<String, String> func4 = (x, y) -> (x + y).length(); // ããããåºå System.out.println(func3.apply("sample1", "sample2")); // 12 System.out.println(func4.applyAsInt("sample", "sample2")); // 12 }
ãã®ä»ã«ãToLongFunctionãIntToDoubleFunctionãã¨ãã£ãå
¨ã¦Functionã§è£ããã®ã«ãããããå¥ã®Interfaceã¨ãã¦å®ç¾©ããã¦ããçç±ãã¾ã£ããããããªãã£ãã
ããã§ããããã調ã¹ã¦ã¿ãã¨åãçµè«ã«ãã©ãçãå 容ã®ãã®ãããã¾ããã
Why doesn't Java 8's ToIntFunction<T> extend Function<T, Integer> - goto: answer
ä¸è¨ã®çåã¯ãªããToIntFunctionã¯Functionãç¶æ¿ããªãã¨ãããã®ã ãã確ãã«ããèãã¦ã¿ãã¨ãä¸è¨ã®ããã«ç¶æ¿ãã¦ãåé¡ãªãã¯ãã
ãããããapplyAsIntãªãã¦ã¡ã½ããã追å ããå¿
è¦ããªãããæ±ç¨æ§ãããã
@FunctionalInterface public interface ToIntFunction<T> extends Function<T, Integer> { // int applyAsInt(T value); @Override default Integer apply(T value) { return Integer.valueOf(applyAsInt(value)); } }
ããã¯Predicateã§ãåæ§ã®ãã¨ãè¨ããã
ãããtestã¨ããã¡ã½ããã§åºå¥ãããå
¨ã¦applyã§é©ç¨åºæ¥ãæ¹ãæ±ç¨çã ãã使ããããã¯ããªã®ã«ãããããFunctionã¨Predicateãå¥ç©ã¨ãã¦å®ç¾©ãã¦ããã
@FunctionalInterface public interface Predicate<T> extends Function<T, Boolean> { // boolean test(T t); @Override default Boolean apply(T t) { return Boolean.valueOf(test(t)); } }
ã§ã¯ããªãç¶æ¿ãããã¾ã£ããå¥ã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ã¨ãã¦Java8ã§å®ç¾©ããã®ãã
ã©ãããããã®åå ã¨ãã¦ã¸ã§ããªãã¯ã¹ã§ã¯primitiveåã使ç¨ã§ãããå¼·å¶çã«ã©ããã¼ã¯ã©ã¹ãã使ããªããã¨ã«èµ·å ãã¦ãããããã
ã¤ã¾ããããã¯ä¸å¯è½ã ã
Function<String, int> myFunction;
ããã¯å¯è½ã¨ãããã¨
Function<String, Integer> myFunction;
ã§ãããã®ä½ãåé¡ãã¨ãã㨠primitiveåã¨ã©ããã¼ã®åç
§åãå©ç¨ãã㨠boxing/unboxing ã¨ãã£ãå¤æå¦çãå½±é¿ãã¦ããã
boxing/unboxing ã¯ã³ã¹ããé«ãä¸ã«ãã©ããã¼ã¯ã©ã¹ã®ãªãã¸ã§ã¯ãçæãå¹çåããããã«ãprimitiveåãæ±ãã¢ã«ã´ãªãºã ãè¤éã«ããªãã¨ãããªãã®ã§é常ã«éå¹çã
ãªã®ã§ããããããboxing/unboxing ãå¿
è¦ã¨ããªãã¤ã³ã¿ã¼ãã§ã¤ã¹ãããããä½ã£ãããã§ãã
ãã®åé¡ã¯Primitive collections support breaks existing codeã§ãè°è«ããã¦ãã¦
Brian Goetzã¨ããlambda projectã®ãªã¼ãã¼ãä¸è¨ã®ãããªå
容ã示ãã¦ãã¾ããã
primitiveç¨ã®Streamã«ã¯ãã¬ã¼ããªããä¼´ããããªãã¹ãããæ¹åã«ãªãããã«æ¤è¨ãã¦ããã
ï¼ï¼8種é¡å ¨ã¦ã®primitiveåã«å¯¾å¿ããªãã
ããint,long,doubleã®ã¿å¯¾å¿ããã
ï¼ï¼primitiveåã§å©ç¨ãããããè¨ç®(sorting,reduction) ãã§ããããã«primitiveåã®Streamãå®ç¾©ãã¾ããå°æ¥(JDK 9)ã§ã¯Value Typesã«ãã£ã¦ãããã®interfaceãåãé¤ããã¨ãã§ãããããããªãã
ã¾ã¨ã
ãããªããã§ãã¤ã³ã¿ã¼ãã§ã¤ã¹ã®æ°ããããªã«ãå¤ããªã£ã¦ãã¾ã£ãçç±ã¨ãã¦ã¯ãå®ã¯çµæ§åç´ã§boxing/unboxingã®ã³ã¹ããå¹çåã«ããåé¡ã«å¯¾å¿ããããã§ããã
çµæprimitiveåã«å¯¾å¿ããå°ç¨ã®ã¤ã³ã¿ã¼ãã§ã¤ã¹ãããããå®ç¾©ãããããã§ãã
åè
http://stackoverflow.com/questions/22690271/why-doesnt-java-8s-tointfunctiont-extend-functiont-integer
http://stackoverflow.com/questions/22682836/why-doesnt-java-8s-predicatet-extend-functiont-boolean
http://gotoanswer.stanford.edu/?q=Why+doesn%27t+Java+8%27s+ToIntFunction%3CT%3E+extend+Function%3CT%2C+Integer%3E