Java 8 从java中的列表中提取列表值 公共类FetchVarableList{ 公共静态void main(字符串[]args){ List empsList=new ArrayList(); 员工e1=新员工(1,“Abi”,“Fin”,2000年); 员工e2=新员工(2名,“Chandu”,“OPs”,5000); 员工e3=新员工(3,“马赫什”,“人力资源”,8000); 员工e4=新员工(4,“Suresh”,“Main”,1000); List empList=new ArrayList(); empList.add(e1);empList.add(e2); List empList2=new ArrayList(); 雇员2.add(e3);雇员2.add(e4); empsList.add(雇员列表); 添加临时名单(雇员2); }

Java 8 从java中的列表中提取列表值 公共类FetchVarableList{ 公共静态void main(字符串[]args){ List empsList=new ArrayList(); 员工e1=新员工(1,“Abi”,“Fin”,2000年); 员工e2=新员工(2名,“Chandu”,“OPs”,5000); 员工e3=新员工(3,“马赫什”,“人力资源”,8000); 员工e4=新员工(4,“Suresh”,“Main”,1000); List empList=new ArrayList(); empList.add(e1);empList.add(e2); List empList2=new ArrayList(); 雇员2.add(e3);雇员2.add(e4); empsList.add(雇员列表); 添加临时名单(雇员2); },java-8,java-stream,Java 8,Java Stream,} 上面的代码在empList中列出了员工e1、e2,在empList2中列出了员工e3、e4。这两个列表将添加到empslist。我想获取所有员工编号并将其存储在一个整数列表中 如何从Java8中的empslist获取员工编号列表?list numbers=empslist.stream() public class FetchVarableList { public static void main(String[] args) { List<List<Emp

}

上面的代码在
empList
中列出了员工e1、e2,在
empList2
中列出了员工e3、e4。这两个列表将添加到
empslist
。我想获取所有员工编号并将其存储在一个整数列表中

如何从Java8中的
empslist
获取员工编号列表?

list numbers=empslist.stream()
public class FetchVarableList {

public static void main(String[] args) {
    
    List<List<Employee>> empsList = new ArrayList<>();

    Employee e1 = new Employee(1, "Abi", "Fin", 2000);
    Employee e2 = new Employee(2, "Chandu", "OPs", 5000);
    Employee e3 = new Employee(3, "mahesh", "HR", 8000);
    Employee e4 = new Employee(4, "Suresh", "Main", 1000);
    
    
    List<Employee> empList = new ArrayList<>();
    empList.add(e1); empList.add(e2); 

    
    List<Employee> empList2 = new ArrayList<>();
    
    empList2.add(e3); empList2.add(e4);

    
    empsList.add(empList);
    
    empsList.add(empList2);
}
.flatMap(列表::流) .map(雇员::getNumber) .collect(Collectors.toList());
列表编号=empsList.stream()
.flatMap(列表::流)
.map(雇员::getNumber)
.collect(Collectors.toList());
列表编号=Stream.concat(empList.Stream(),empList2.Stream())
.map(雇员::getNumber)
.collect(Collectors.toList());
列表编号=Stream.concat(empList.Stream(),empList2.Stream())
.map(雇员::getNumber)
.collect(Collectors.toList());
结合JB Nizet和ahmet kamaran的答案,您可以通过连接两个列表来避免创建列表列表(
empsList
)的步骤:

List<Integer> numbers = Stream.concat(empList.stream(), empList2.stream())
     .map(Employee::getNumber)
     .collect(Collectors.toList());
List number=Stream.concat(empList.Stream(),empList2.Stream())
.map(雇员::getNumber)
.collect(Collectors.toList());
结合JB Nizet和ahmet kamaran的答案,您可以通过连接两个列表来避免创建列表列表(
empsList
)的步骤:

List<Integer> numbers = Stream.concat(empList.stream(), empList2.stream())
     .map(Employee::getNumber)
     .collect(Collectors.toList());
List number=Stream.concat(empList.Stream(),empList2.Stream())
.map(雇员::getNumber)
.collect(Collectors.toList());

你在这方面做了哪些尝试?你在这方面做了哪些尝试?你的代码甚至无法编译<未定义代码>empsList2。即使在更改为
empList2
时,它仍然失败,其中有
empList
empList2
变量存在问题
empsList2
应该是
empList2
您的代码甚至无法编译<未定义代码>empsList2。即使在更改为
empList2
时,它仍然失败,其中有
empList
empList2
变量存在问题<代码>empsList2应该是
empList2
List<Integer> numbers = Stream.concat(empList.stream(), empList2.stream())
     .map(Employee::getNumber)
     .collect(Collectors.toList());