java stream map,實作将對象list轉為單屬性list。
直接上代碼。
List<String> collect = dataItemList.stream()
.map(item -> {
return item.getMobile();
})
.collect(Collectors.toList());
可見,通過return,可轉換為各種類型的。
如寫成
return new AAA(){...}
這樣來控制類型。
如:
dataItemList.stream()
.map(item -> {
return new TempModel(...);
})
.collect(Collectors.toList());

List<String> collect = dataItemList.stream()
.map(item -> item.getMobile())
.collect(Collectors.toList());