lambda 表达式合集

Cheems Lv3

来自java8的“新”特性…

lambda in java

1.1 常见操作

1.1.1 创建流

单列集合 : 集合对象.stream();

1
2
3
4
5
6
7
8
9
class Obj{
propertie1;
propertie2;
...etc...
}
public static void main(String[] args) {
List<obj.class> listObj = getList();
Stream<obj.class> streamObj = listObj.stream();
}

数组: Arrays.stream(数组对象) 或者使用 Stream.of创建

1
2
3
Integer[] arr = {1,2,3,4,5};
Stream<Integer> stream1 = Arrays.stream(arr);
Stream<Integer> stream2 = Stream.of(arr);

双列集合: 转换成单列集合在创建流(就是套娃)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Map<String, Integer> map = new HashMap<>();
map.put("test1 " , 15);
map.put("test2 " , 16);
map.put("test3 " , 17);

//把map 双列集合转换为单列集合
Set<Map.Entry<String, Integer>> entrySet = map.entrySet();

entrySet.stream().filter(new Predicate<Map.Entry<String, Integer>>() {
@Override
public boolean test(Map.Entry<String, Integer> stringIntegerEntry) {
return stringIntegerEntry.getValue()>15;
}
}).forEach(new Consumer<Map.Entry<String, Integer>>() {
@Override
public void accept(Map.Entry<String, Integer> stringIntegerEntry) {
System.out.println(stringIntegerEntry.getKey() + "===" + stringIntegerEntry.getValue());
}
});

1.1.2 中间操作

  • Title: lambda 表达式合集
  • Author: Cheems
  • Created at : 2022-04-25 20:37:50
  • Updated at : 2023-05-02 18:11:52
  • Link: https://processmonitor.github.io/2022/04/25/Lambda表达式相关/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments