对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;
	}


}

  

posted @ 2020-06-12 09:30  margo  阅读(50)  评论(0编辑  收藏  举报