1. 进行List类型转换
我们常会遇到这样一种问题,就是遍历List,将A中的某个元素赋给B,形成一个List。传统的编码方式应该是这样的:
```java
List s1List = Stream.of(new S1("1", "Lee"), new S1("2", "Wang")).collect(Collectors.toList());
List s2List = new ArrayList<>();
for (S1 s1 : s1List) {
S2 s2 = new S2(s1.getId());
s2List.add(s2);
}
System.out.println(s2List);
```java
List<S1> s1List = Stream.of(new S1("1", "Lee"), new S1("2", "Wang")).collect(Collectors.toList());
List<S2> s2List = s1List.stream().map(s1 -> new S2(s1.getId())).collect(Collectors.toList());
System.out.println(s2List);
同样的逻辑,只需要一行代码就搞定了。显着那么的优雅,Java也有点Python的意思了。