Java: FunctionalInterface
作者:互联网
java.util.function 定义了一些预定义FuncationInterface
Supplier:
import java.util.function.Supplier; public class addThen{ public static void main(String[] args){ for(int i = 0; i < 10; i++){ jot(() -> (int) Math.floor(Math.random() * 5 + 1)); } } // 生成 Supplier 生成 [1,5]间随机数 public static void jot(Supplier<Integer> supplier){ Integer integer = supplier.get(); System.out.println("integer = " + integer); } }
Consumer:
import java.util.Locale; import java.util.function.Consumer; public class addThen{ public static void main(String[] args){ jot(s->{ System.out.println("s = " + s); System.out.println("s.toUpperCase(Locale.ROOT) = " + s.toUpperCase(Locale.ROOT)); },s->{ System.out.println("s = " + s); System.out.println("s.toLowerCase(Locale.ROOT) = " + s.toLowerCase(Locale.ROOT)); }); } public static void jot(Consumer<String> c1, Consumer<String> c2){ c1.andThen(c2).accept("etymology"); } }
Function:
import java.util.function.Function; public class addThen{ public static void main(String[] args){ jot(Integer::parseInt, "55"); } // String -> Integer private static void jot(Function<String, Integer> function, String s){ Integer integer = function.apply(s); System.out.println("integer = " + integer); } }
import java.util.function.Function; public class addThen{ public static void main(String[] args){ jot(Integer::parseInt, Integer::doubleValue, "55"); } // String -> Integer -> Double private static void jot(Function<String, Integer> f1, Function<Integer, Double> f2, String s){ Double aDouble = f1.andThen(f2).apply(s); System.out.println("aDouble = " + aDouble); } }
import java.util.function.Function; public class addThen{ public static void main(String[] args){ jot(Integer::parseInt, Integer::doubleValue, "55"); } // String -> Integer -> Double // 与andThen处理顺序不同, 先调用f1, 把f1结果作为参数给f2 private static void jot(Function<String, Integer> f1, Function<Integer, Double> f2, String s){ Double aDouble = f2.compose(f1).apply(s); System.out.println("aDouble = " + aDouble); } }
import java.util.function.Function; public class addThen{ public static void main(String[] args){ Function<Integer, Integer> identity = Function.identity(); Integer integer = identity.apply(5); System.out.println("integer = " + integer); Integer modulus = identity.andThen(i -> i % 3).andThen(i -> (int) Math.pow(i, 3)).apply(11); System.out.println("modulus = " + modulus); } }
Predicate:
import java.util.function.Predicate; public class addThen{ public static void main(String[] args){ jot(s -> s.contains("jot"), "jotq"); } private static void jot(Predicate<String> predicate, String message){ boolean flag = predicate.test(message); System.out.println("flag = " + flag); } }
import java.util.function.Predicate; public class addThen{ public static void main(String[] args){ jot(s->s.contains("jot"),s->s.contains("qw"),"jotqwer"); } private static void jot(Predicate<String> p1,Predicate<String> p2, String message){ boolean flag = p1.and(p2).test(message); System.out.println("flag = " + flag); } }
import java.util.function.Predicate; public class addThen{ public static void main(String[] args){ jot(s -> s.contains("jot"), s -> s.contains("qw"), "jotqwer"); } private static void jot(Predicate<String> p1, Predicate<String> p2, String message){ boolean flag = p1.and(p2).negate().test(message); System.out.println("flag = " + flag); } }
import java.util.function.Predicate; public class addThen{ public static void main(String[] args){ System.out.println("Predicate.isEqual(null).test(null) = " + Predicate.isEqual(null).test(null)); System.out.println("Predicate.isEqual(\"jot\").test(\"jot\") = " + Predicate.isEqual("jot").test("jot")); } }
import java.util.function.Predicate; public class addThen{ public static void main(String[] args){ boolean flag = Predicate.not(new Predicate<String>(){ @Override public boolean test(String s){ return s.contains("jot"); } }).test("jotqw"); System.out.println("flag = " + flag); } }
标签:Predicate,jot,String,void,FunctionalInterface,static,Java,public 来源: https://www.cnblogs.com/dissipate/p/16308886.html