使用stream.map()提取List对象中的某一字段

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用stream.map()提取List对象中的某一字段相关的知识,希望对你有一定的参考价值。

参考技术A ** List对象类(StudentInfo)**

** 测试数据 **

** 提取某一列(以name为例)**

输出结果如图所示:

jdk8的特性stream().map()

在Java 8中stream().map(),您可以将对象转换为其他对象

1.大写字符串列表


 
package com.mkyong.java8;
 
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
public class TestJava8 {
 
    public static void main(String[] args) {
 
        List<String> alpha = Arrays.asList("a", "b", "c", "d");
 
        //Before Java8
        List<String> alphaUpper = new ArrayList<>();
        for (String s : alpha) {
            alphaUpper.add(s.toUpperCase());
        }
 
        System.out.println(alpha); //[a, b, c, d]
        System.out.println(alphaUpper); //[A, B, C, D]
 
        // Java 8
        List<String> collect = alpha.stream().map(String::toUpperCase).collect(Collectors.toList());
        System.out.println(collect); //[A, B, C, D]
 
        // Extra, streams apply to any data type.
        List<Integer> num = Arrays.asList(1,2,3,4,5);
        List<Integer> collect1 = num.stream().map(n -> n * 2).collect(Collectors.toList());
        System.out.println(collect1); //[2, 4, 6, 8, 10]
 
    }
 
}

2.对象列表 - >字符串列表

package com.mkyong.java8;
 
import java.math.BigDecimal;
 
public class Staff {
 
    private String name;
    private int age;
    private BigDecimal salary;
    //...
}
 
TestJava8.java
 
package com.mkyong.java8;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
public class TestJava8 {
 
    public static void main(String[] args) {
 
        List<Staff> staff = Arrays.asList(
                new Staff("mkyong", 30, new BigDecimal(10000)),
                new Staff("jack", 27, new BigDecimal(20000)),
                new Staff("lawrence", 33, new BigDecimal(30000))
        );
 
        //Before Java 8
        List<String> result = new ArrayList<>();
        for (Staff x : staff) {
            result.add(x.getName());
        }
        System.out.println(result); //[mkyong, jack, lawrence]
 
        //Java 8
        List<String> collect = staff.stream().map(x -> x.getName()).collect(Collectors.toList());
        System.out.println(collect); //[mkyong, jack, lawrence]
 
    }
 
}
 

对象列表 - >其他对象列表

3.1此示例说明如何将staff对象列表转换为对象列表StaffPublic。

package com.mkyong.java8;
 
import java.math.BigDecimal;
 
public class Staff {
 
    private String name;
    private int age;
    private BigDecimal salary;
    //...
}
 
StaffPublic.java
 
package com.mkyong.java8;
 
public class StaffPublic {
 
    private String name;
    private int age;
    private String extra;
    //...
}
 
3.2 Java 8之前。
 
BeforeJava8.java
 
package com.mkyong.java8;
 
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
public class BeforeJava8 {
 
    public static void main(String[] args) {
 
        List<Staff> staff = Arrays.asList(
                new Staff("mkyong", 30, new BigDecimal(10000)),
                new Staff("jack", 27, new BigDecimal(20000)),
                new Staff("lawrence", 33, new BigDecimal(30000))
        );
 
        List<StaffPublic> result = convertToStaffPublic(staff);
        System.out.println(result);
 
    }
 
    private static List<StaffPublic> convertToStaffPublic(List<Staff> staff) {
 
        List<StaffPublic> result = new ArrayList<>();
 
        for (Staff temp : staff) {
 
            StaffPublic obj = new StaffPublic();
            obj.setName(temp.getName());
            obj.setAge(temp.getAge());
            if ("mkyong".equals(temp.getName())) {
                obj.setExtra("this field is for mkyong only!");
            }
 
            result.add(obj);
        }
 
        return result;
 
    }
 
}

Java 8的例子

package com.mkyong.java8;
 
package com.hostingcompass.web.java8;
 
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
 
public class NowJava8 {
 
    public static void main(String[] args) {
 
        List<Staff> staff = Arrays.asList(
                new Staff("mkyong", 30, new BigDecimal(10000)),
                new Staff("jack", 27, new BigDecimal(20000)),
                new Staff("lawrence", 33, new BigDecimal(30000))
        );
 
        // convert inside the map() method directly.
        List<StaffPublic> result = staff.stream().map(temp -> {
            StaffPublic obj = new StaffPublic();
            obj.setName(temp.getName());
            obj.setAge(temp.getAge());
            if ("mkyong".equals(temp.getName())) {
                obj.setExtra("this field is for mkyong only!");
            }
            return obj;
        }).collect(Collectors.toList());
 
        System.out.println(result);
 
    }
 
}
 
 
[
    StaffPublic{name='mkyong', age=30, extra='this field is for mkyong only!'},
    StaffPublic{name='jack', age=27, extra='null'},
    StaffPublic{name='lawrence', age=33, extra='null'}
]

以上是关于使用stream.map()提取List对象中的某一字段的主要内容,如果未能解决你的问题,请参考以下文章

返回stream.map中定义的函数的模拟对象

Lambda表达式

jdk8的特性stream().map()

jdk8的特性stream().map()

list中是map,stream如何拿到key集合

如何提取DataTable中的某一列字段的所有数据,进行运算