Sorted
根据字符长短排序
java">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);});//lambdaList<String> list2 = list.stream().sorted((l1, l2) -> l1.length() - l2.length()).collect(Collectors.toList());list2.forEach(l -> {System.out.println(l);});}
}
BiFunction example
java">public class Java8Demo1 {public static void main(String[] args) {// Try BiFunctionMyCalculator<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);}
}
输出
java">5.0
18.0
6.0
2.0Process finished with exit code 0