天天看点

java stream 将List<Object>转换为 List<String>,类似C#的LINQ的select()方法

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());      
java stream 将List<Object>转换为 List<String>,类似C#的LINQ的select()方法
List<String> collect = dataItemList.stream()
            .map(item -> item.getMobile())
            .collect(Collectors.toList());