Java8-Lambda-No.02

mac2022-06-30  97

public class Lambda2 { @FunctionalInterface public static interface Converter<F, T> { T convert(F from); } static class Something { String startsWith(String s) { return String.valueOf(s.charAt(0)); } } interface PersonFactory<P extends Person> { P create(String firstName, String lastName); } public static void main(String[] args) { Converter<String, Integer> integerConverter1 = (from) -> Integer.valueOf(from); Integer converted1 = integerConverter1.convert("123"); System.out.println(converted1); // result: 123 // method reference Converter<String, Integer> integerConverter2 = Integer::valueOf; Integer converted2 = integerConverter2.convert("123"); System.out.println(converted2); // result: 123 Something something = new Something(); Converter<String, String> stringConverter = something::startsWith; String converted3 = stringConverter.convert("Java"); System.out.println(converted3); // result J // constructor reference PersonFactory<Person> personFactory = Person::new; Person person = personFactory.create("Peter", "Parker"); } }

转载于:https://www.cnblogs.com/bilaisheng/p/10210916.html

相关资源:Java8视频新特性 百度云
最新回复(0)