赞
踩
public class Java8Demo1 { public static void main(String[] args) { // Sort by length of the words. List<String> list = Arrays.asList("1234","456","abefc"); List<String> list1 = list.stream() .sorted(Comparator.comparing(x -> x.length())) .collect(Collectors.toList()); list1.forEach(l -> { System.out.println(l); }); //lambda List<String> list2 = list.stream() .sorted((l1, l2) -> l1.length() - l2.length()) .collect(Collectors.toList()); list2.forEach(l -> { System.out.println(l); }); } }
public class Java8Demo1 { public static void main(String[] args) { // Try BiFunction MyCalculator<Double> calculator = new MyCalculator(); // + System.out.println(calculator.compute(2.0, 3.0, (a, b) -> a + b)); // - System.out.println(calculator.compute(21.0, 3.0, (a, b) -> a - b)); // * System.out.println(calculator.compute(2.0, 3.0, (a, b) -> a * b)); // / System.out.println(calculator.compute(6.0, 3.0, (a, b) -> a / b)); } } class MyCalculator<T> { public T compute(T t1, T t2, BiFunction<T,T,T> function) { return function.apply(t1, t2); } }
输出
5.0
18.0
6.0
2.0
Process finished with exit code 0
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。