如何在使用流(Streams)时访问相应的内部列表项

huangapple go评论77阅读模式
英文:

How to access Corresponding Inner List Item while using Streams

问题

这是我给您翻译好的内容:

public static void main(String[] args) {
    String json = "您的 JSON 数据"; // 这里是您的 JSON 数据,由于长度较长,我无法直接显示,您可以将原始 JSON 数据放在这里。

    Gson gson = new Gson();
    Root empResp = gson.fromJson(json, Root.class);
    
    Optional.ofNullable(empResp)
            .map(Root::getEmployees)
            .map(Employees::getEmp)
            .orElseGet(Collections::emptyList)
            .stream()
            .forEach(emp -> {
                int empId = emp.getEmpidid();
                String empName = emp.getEmpname();
                List<Dept> departments = emp.getDepartments().getDept();
                
                for (Dept dept : departments) {
                    String deptCode = dept.getCode();
                    // 在这里使用 empId, empName, deptCode 做相应处理
                }
            });
}

请注意,由于您的 JSON 数据较长,我无法在单个回答中显示完整的 JSON 数据和完整的类结构,因此我在上述代码中将 JSON 数据的部分用 "您的 JSON 数据" 来代替。请确保您将实际的 JSON 数据和类结构替换到相应的位置。

此代码演示了如何在循环 emp 列表时访问相应的部门代码(dept code)。在循环 emp 列表的过程中,我们首先获取每个 emp 对象的 ID、姓名和部门列表,然后再在部门列表中循环以获取部门代码。您可以在标有注释的部分执行您需要的操作。

英文:

I have the following JSON Structure

{
	&quot;employees&quot;: {
		&quot;emp&quot;: [{
				&quot;departments&quot;: {
					&quot;dept&quot;: [{
						&quot;code&quot;: &quot;S&quot;,
						&quot;description&quot;: &quot;FieldWork&quot;
					}]
				},
				&quot;empidid&quot;: 35,
				&quot;empname&quot;: &quot;Mark&quot;
			}

		]
	}
}

Each emp is a List which has corresponding dept List
While looping emp as a List , i need corresponding Dept Code also

I am facing trouble getting the corresponding dept code while looping

public static void main(String[] args) {
		
		
		String json = &quot;{\r\n&quot; + 
				&quot;	\&quot;employees\&quot;: {\r\n&quot; + 
				&quot;		\&quot;emp\&quot;: [{\r\n&quot; + 
				&quot;				\&quot;departments\&quot;: {\r\n&quot; + 
				&quot;					\&quot;dept\&quot;: [{\r\n&quot; + 
				&quot;						\&quot;code\&quot;: \&quot;S\&quot;,\r\n&quot; + 
				&quot;						\&quot;description\&quot;: \&quot;FieldWork\&quot;\r\n&quot; + 
				&quot;					}]\r\n&quot; + 
				&quot;				},\r\n&quot; + 
				&quot;				\&quot;empidid\&quot;: 35,\r\n&quot; + 
				&quot;				\&quot;empname\&quot;: \&quot;Mark\&quot;\r\n&quot; + 
				&quot;			}\r\n&quot; + 
				&quot;\r\n&quot; + 
				&quot;		]\r\n&quot; + 
				&quot;	}\r\n&quot; + 
				&quot;}&quot;;
		
		Gson gson = new Gson();
		RootVal empResp = gson.fromJson(json, RootVal.class);
		
		Optional.ofNullable(empResp)
		.map(e -&gt; e.getEmployees())
		.map(e -&gt; e.getEmp())
		
		.orElseGet(Collections::emptyList).stream().map(emp -&gt; {
			
			emp.getEmpidid();
			emp.getEmpidid();
			// How to get DEPT Code Here 
			return null;
		}).collect(Collectors.toList());
		
		

	}

This is my class Structure

==================================

public class Dept
{
    private String code;

    private String description;

    public void setCode(String code){
        this.code = code;
    }
    public String getCode(){
        return this.code;
    }
    public void setDescription(String description){
        this.description = description;
    }
    public String getDescription(){
        return this.description;
    }
}

==================================

import java.util.ArrayList;
import java.util.List;
public class Departments
{
    private List&lt;Dept&gt; dept;

    public void setDept(List&lt;Dept&gt; dept){
        this.dept = dept;
    }
    public List&lt;Dept&gt; getDept(){
        return this.dept;
    }
}

==================================

public class Emp
{
    private Departments departments;

    private int empidid;

    private String empname;

    public void setDepartments(Departments departments){
        this.departments = departments;
    }
    public Departments getDepartments(){
        return this.departments;
    }
    public void setEmpidid(int empidid){
        this.empidid = empidid;
    }
    public int getEmpidid(){
        return this.empidid;
    }
    public void setEmpname(String empname){
        this.empname = empname;
    }
    public String getEmpname(){
        return this.empname;
    }
}

==================================

import java.util.ArrayList;
import java.util.List;
public class Employees
{
    private List&lt;Emp&gt; emp;

    public void setEmp(List&lt;Emp&gt; emp){
        this.emp = emp;
    }
    public List&lt;Emp&gt; getEmp(){
        return this.emp;
    }
}

==================================

public class Root
{
    private Employees employees;

    public void setEmployees(Employees employees){
        this.employees = employees;
    }
    public Employees getEmployees(){
        return this.employees;
    }
}

Please let me how to access Corresponding Dept Code while looping emp List

答案1

得分: 2

你可以使用 Emp 类的字段获取方法,类似这样:

emp -> emp.getDepartments().getDept()

在这里,getDept() 返回 List<Dept>,所以要获取 code,你可以对列表中的每个 Dept 对象使用获取方法,例如 .get(0).getCode(),以获取 Dept 列表中第一个元素的 code

完整代码:

Optional.ofNullable(empResp)
    .map(e -> e.getEmployees())
    .map(e -> e.getEmp())
        
    .orElseGet(Collections::emptyList).stream().map(emp -> {
        emp.getEmpidid();
        emp.getEmpidid();
        // 在这里如何获取 DEPT Code
        emp.getDepartments().getDept().get(0).getCode();
        return null;
    }).collect(Collectors.toList());
英文:

You can use the field's getter method of Emp class and so on like

emp -&gt; emp.getDepartments().getDept()

Here, getDept() return List&lt;Dept&gt; so to get code, you can use getter for each Dept object of list. Like .get(0).getCode() to get first element's code of Dept list.

Full code:

Optional.ofNullable(empResp)
    .map(e -&gt; e.getEmployees())
    .map(e -&gt; e.getEmp())
    
    .orElseGet(Collections::emptyList).stream().map(emp -&gt; {
        emp.getEmpidid();
        emp.getEmpidid();
        // How to get DEPT Code Here 
        emp.getDepartments().getDept().get(0).getCode();
        return null;
    }).collect(Collectors.toList());

huangapple
  • 本文由 发表于 2020年7月25日 00:27:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63077682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定