标签:and 类型 public 判断 ble 不同类 hello cep void
接口 | 参数 | 返回类型 | 描述 |
Predicate<T> | T | boolean | 用于判断一个对象. |
Consumer<T> | T | void | 用于接收一个对象进行处理但没有返回. |
Function<T,R> | T | R | 转换一个对象为不同类型的对象 |
Supplier<T> | None | T | 提供一个对象 |
UnaryOperator<T> | T | T | 接收对象并返回同类型对象 |
BinaryOperator<T> | (T,T) | T | 接收两个同类型对象,并返回一个原类型对象 |
public static void main(String[] args) { Predicate<String> p1 = str -> !str.isEmpty(); Predicate<String> p2 = str -> str.endsWith("d"); boolean test = p1.and(p2).test("end"); System.out.println(test); }
private static void consumeString(Consumer<String> one, Consumer<String> two) { one.andThen(two).accept("Hello"); } public static void main(String[] args) { consumeString( s -> System.out.println(s.toUpperCase()), s -> System.out.println(s.toLowerCase()));
}
public static void main(String[] args) { method(s->Integer.parseInt(s)); method(Integer::parseInt); } private static void method(Function<String,Integer> function){ int num=function.apply("10"); System.out.println(num+20); }
private static String getString(Supplier<String> function) { return function.get(); } public static void main(String[] args) { String msgA = "Hello"; String msgB = "World"; System.out.println(getString(() -> msgA + msgB)); }
public static void main(String[] args) { //创建一个 UnaryOperator对象, UnaryOperator operator = UnaryOperator.identity(); //调用 apply()方法,输出参数的值 System.out.println("输出与输出一样:" + operator.apply("abc")); }
public static void main(String[] args) { BinaryOperator<Long> functionAdd = (Long x, Long y) -> x + y; Long res = functionAdd.apply(1L, 2L); }
调用特定方法的Lambda表达式的一种快捷写法,可以让你重复使用现有的方法定义,并像Lambda表达式一样传递他们.
/** * (args) -> ClassName.staticMethod(args); * ClassName::staticMethod; */ @Test public void test1() { Consumer<String> c1 = (String number) -> Integer.parseInt(number); Consumer<String> c2 = Integer::parseInt; }
/** * (args) -> args.instanceMethod(); * ClassName::instanceMethod; */ @Test public void test2() { Consumer<String> c1 = (String str) -> str.length(); Consumer<String> c2 = String::length; }
/** * (args) -> object.instanceMethod(args); * object::instanceMethod; */ @Test public void test3() { StringBuilder stringBuilder = new StringBuilder(); Consumer<String> c1 = (String str) -> stringBuilder.append(str); Consumer<String> c2 = stringBuilder::append; }
标签:and 类型 public 判断 ble 不同类 hello cep void
原文地址:https://www.cnblogs.com/mingmingn/p/13058225.html