对lambda一个代码的思考
class Try { public void test() { Comparator<Integer> comparator = (o1,o2) -> Integer.compare(o1,o2); Comparator<Integer> comparator1 = new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return Integer.compare(o1,o2); } }; Set<Integer> set = new TreeSet<>(new Comparator<Integer>() { @Override public int compare(Integer o1, Integer o2) { return Integer.compare(o1,o2); } }); } public static <T, R> Function<T, R> of(CheckFunction<T, R> mapper) { Objects.requireNonNull(mapper); return new Function<T, R>() { @Override public R apply(T t) { try { return mapper.apply(t); } catch (Exception e) { e.printStackTrace(); } return null; } }; } @FunctionalInterface public interface CheckFunction<T, R>{ R apply(T t) throws Exception; } }
本文来自博客园,作者:margo,转载请注明原文链接:https://www.cnblogs.com/ZMargo/articles/13097589.html