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

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

How to access Corresponding Inner List Item while using Streams

问题

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

  1. public static void main(String[] args) {
  2. String json = "您的 JSON 数据"; // 这里是您的 JSON 数据,由于长度较长,我无法直接显示,您可以将原始 JSON 数据放在这里。
  3. Gson gson = new Gson();
  4. Root empResp = gson.fromJson(json, Root.class);
  5. Optional.ofNullable(empResp)
  6. .map(Root::getEmployees)
  7. .map(Employees::getEmp)
  8. .orElseGet(Collections::emptyList)
  9. .stream()
  10. .forEach(emp -> {
  11. int empId = emp.getEmpidid();
  12. String empName = emp.getEmpname();
  13. List<Dept> departments = emp.getDepartments().getDept();
  14. for (Dept dept : departments) {
  15. String deptCode = dept.getCode();
  16. // 在这里使用 empId, empName, deptCode 做相应处理
  17. }
  18. });
  19. }

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

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

英文:

I have the following JSON Structure

  1. {
  2. &quot;employees&quot;: {
  3. &quot;emp&quot;: [{
  4. &quot;departments&quot;: {
  5. &quot;dept&quot;: [{
  6. &quot;code&quot;: &quot;S&quot;,
  7. &quot;description&quot;: &quot;FieldWork&quot;
  8. }]
  9. },
  10. &quot;empidid&quot;: 35,
  11. &quot;empname&quot;: &quot;Mark&quot;
  12. }
  13. ]
  14. }
  15. }

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

  1. public static void main(String[] args) {
  2. String json = &quot;{\r\n&quot; +
  3. &quot; \&quot;employees\&quot;: {\r\n&quot; +
  4. &quot; \&quot;emp\&quot;: [{\r\n&quot; +
  5. &quot; \&quot;departments\&quot;: {\r\n&quot; +
  6. &quot; \&quot;dept\&quot;: [{\r\n&quot; +
  7. &quot; \&quot;code\&quot;: \&quot;S\&quot;,\r\n&quot; +
  8. &quot; \&quot;description\&quot;: \&quot;FieldWork\&quot;\r\n&quot; +
  9. &quot; }]\r\n&quot; +
  10. &quot; },\r\n&quot; +
  11. &quot; \&quot;empidid\&quot;: 35,\r\n&quot; +
  12. &quot; \&quot;empname\&quot;: \&quot;Mark\&quot;\r\n&quot; +
  13. &quot; }\r\n&quot; +
  14. &quot;\r\n&quot; +
  15. &quot; ]\r\n&quot; +
  16. &quot; }\r\n&quot; +
  17. &quot;}&quot;;
  18. Gson gson = new Gson();
  19. RootVal empResp = gson.fromJson(json, RootVal.class);
  20. Optional.ofNullable(empResp)
  21. .map(e -&gt; e.getEmployees())
  22. .map(e -&gt; e.getEmp())
  23. .orElseGet(Collections::emptyList).stream().map(emp -&gt; {
  24. emp.getEmpidid();
  25. emp.getEmpidid();
  26. // How to get DEPT Code Here
  27. return null;
  28. }).collect(Collectors.toList());
  29. }

This is my class Structure

  1. ==================================
  2. public class Dept
  3. {
  4. private String code;
  5. private String description;
  6. public void setCode(String code){
  7. this.code = code;
  8. }
  9. public String getCode(){
  10. return this.code;
  11. }
  12. public void setDescription(String description){
  13. this.description = description;
  14. }
  15. public String getDescription(){
  16. return this.description;
  17. }
  18. }
  19. ==================================
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. public class Departments
  23. {
  24. private List&lt;Dept&gt; dept;
  25. public void setDept(List&lt;Dept&gt; dept){
  26. this.dept = dept;
  27. }
  28. public List&lt;Dept&gt; getDept(){
  29. return this.dept;
  30. }
  31. }
  32. ==================================
  33. public class Emp
  34. {
  35. private Departments departments;
  36. private int empidid;
  37. private String empname;
  38. public void setDepartments(Departments departments){
  39. this.departments = departments;
  40. }
  41. public Departments getDepartments(){
  42. return this.departments;
  43. }
  44. public void setEmpidid(int empidid){
  45. this.empidid = empidid;
  46. }
  47. public int getEmpidid(){
  48. return this.empidid;
  49. }
  50. public void setEmpname(String empname){
  51. this.empname = empname;
  52. }
  53. public String getEmpname(){
  54. return this.empname;
  55. }
  56. }
  57. ==================================
  58. import java.util.ArrayList;
  59. import java.util.List;
  60. public class Employees
  61. {
  62. private List&lt;Emp&gt; emp;
  63. public void setEmp(List&lt;Emp&gt; emp){
  64. this.emp = emp;
  65. }
  66. public List&lt;Emp&gt; getEmp(){
  67. return this.emp;
  68. }
  69. }
  70. ==================================
  71. public class Root
  72. {
  73. private Employees employees;
  74. public void setEmployees(Employees employees){
  75. this.employees = employees;
  76. }
  77. public Employees getEmployees(){
  78. return this.employees;
  79. }
  80. }

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

答案1

得分: 2

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

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

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

完整代码:

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

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

  1. 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:

  1. Optional.ofNullable(empResp)
  2. .map(e -&gt; e.getEmployees())
  3. .map(e -&gt; e.getEmp())
  4. .orElseGet(Collections::emptyList).stream().map(emp -&gt; {
  5. emp.getEmpidid();
  6. emp.getEmpidid();
  7. // How to get DEPT Code Here
  8. emp.getDepartments().getDept().get(0).getCode();
  9. return null;
  10. }).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:

确定