Java Lambda

Lambda


<http://naotu.baidu.com/file/6f48a8bf61f964bd63b986bdb83c2e79?token=11d0ce93b785bbf2>

示例

单词转大写

1
2
3
4
String sentence = "Start Using Java Lambda Expressions";
List<String> words = Arrays.stream(sentence.split(" "))
.map(String::toUpperCase)
.collect(toList());

获取字符串的单词长度和

1
2
3
4
5
String sentence = "Start Using Java Lambda Expressions";
int length = Arrays.stream(sentence.split(" "))
.map(String::length)
.reduce(0, Integer::sum)
.intValue();;

奇偶数分组

1
Map<Boolean, List<Integer>> res = Arrays.asList(1, 2, 3, 4, 5, 6).stream().collect(partitioningBy(i -> (i % 2 == 0)));

1
2
Object[] args = ...;
Arrays.stream(args).map(String::valueOf).collect(Collectors.joining(", ", "(", ")"));
本站采用「署名 4.0 国际」进行许可。