将对象数组添加到数组列表中,并根据一个 属性 值与其他数组列表进行比较并过滤结果
Adding array of object into array list and compare with other array list based on one property value and filter the result
将对象数组添加到数组列表中,并根据一个 属性 值与其他数组列表进行比较并过滤结果。
我有一组对象 personDetails 和 departmentDetails。
package com.education;
import java.util.*;
public class educationMain {
public static void main(String[] args) {
List<person> list=new ArrayList<person>();
person l1 = new person(1,"Samual",100,"Sales","Business");
person l2 = new person(2,"Alex",100,"Sales","Business");
person l3 = new person(3,"Bob",101,"Engineering","Technology");
person l4 = new person(4,"Michel",101,"Engineering","Technology");
person l5 = new person(5,"Ryan",102,"PR","Services");
person l6 = new person(6,"Horward",103,"Leadership","Managmnet");
person l7 = new person(7,"Cyna",104,"HR","Human Resource");
list.add(l1);
list.add(l2);
list.add(l3);
list.add(l4);
list.add(l5);
list.add(l6);
list.add(l7);
for(person b:list){
System.out.println(b.personId+" "+b.name+" "+b.deptCode+" "+b.parentDept+" "+b.deptName);
}
List<department> depList = new ArrayList<department>();
department d1 = new department(100, "Sales","Business");
department d2 = new department(101, "Engineering","Technology");
department d3 = new department(102, "PR","Services");
depList.add(d1);
depList.add(d2);
depList.add(d3);
for(department b:depList){
System.out.println(b.deptCode+" "+b.parentDept+" "+b.deptName);
}
}
}
所以上面的代码工作正常并且显示正确。
我的人class
包 com.education;
public class person {
public int personId;
public String name;
private int deptCode;
private String parentDept;
private String deptName;
public person(int personId, String name, int deptCode, String parentDept, String deptName) {
super();
this.personId = personId;
this.name = name;
this.deptCode = deptCode;
this.parentDept = parentDept;
this.deptName = deptName;
}
public void display(){
System.out.println(" " + personId + " " +name + " " + deptCode+ " " + parentDept + " "+deptName);
System.out.println();
}
}
我的部门class
public class department {
private int deptCode;
private String parentDept;
private String deptName;
public department(int deptCode, String parentDept, String deptName) {
super();
this.deptCode = deptCode;
this.parentDept = parentDept;
this.deptName = deptName;
}
public void dispalyDepartment() {
System.out.println(" "+deptCode+" "+parentDept+" "+deptName);
}
}
现在我的目标是将 personDetails 和 departmentDetails 放入数组列表中并进行比较,然后根据部门代码找出差异。
这是我的逻辑:工作正常。
List<person> listC = new ArrayList<person>();
for(person p : list) {
boolean flag = false;
for (department d:depList) {
if(p.deptCode == d.deptCode) {
flag = false;
break;
}else {
flag = true;
}
}
if(flag == true) {
listC.add(p);
}
}
o/p应该是
(6,"Horward",103,"Leadership","Managmnet");
(7,"Cyna",104,"HR","Human Resource");
因为 deptCode : 103 和 104 不存在。
谁能帮帮我。我可以使用任何其他收集技术吗?
试试这个。
Set<Integer> deptCodes = Arrays.stream(departmentDetails)
.map(department -> department.deptCode)
.collect(Collectors.toSet());
List<person> persons = Arrays.stream(personDetails)
.filter(person -> !deptCodes.contains(person.deptCode))
.collect(Collectors.toList());
persons.forEach(person -> person.display());
输出:
6 Horward 103 Leadership Managmnet
7 Cyna 104 HR Human Resource
将对象数组添加到数组列表中,并根据一个 属性 值与其他数组列表进行比较并过滤结果。
我有一组对象 personDetails 和 departmentDetails。
package com.education;
import java.util.*;
public class educationMain {
public static void main(String[] args) {
List<person> list=new ArrayList<person>();
person l1 = new person(1,"Samual",100,"Sales","Business");
person l2 = new person(2,"Alex",100,"Sales","Business");
person l3 = new person(3,"Bob",101,"Engineering","Technology");
person l4 = new person(4,"Michel",101,"Engineering","Technology");
person l5 = new person(5,"Ryan",102,"PR","Services");
person l6 = new person(6,"Horward",103,"Leadership","Managmnet");
person l7 = new person(7,"Cyna",104,"HR","Human Resource");
list.add(l1);
list.add(l2);
list.add(l3);
list.add(l4);
list.add(l5);
list.add(l6);
list.add(l7);
for(person b:list){
System.out.println(b.personId+" "+b.name+" "+b.deptCode+" "+b.parentDept+" "+b.deptName);
}
List<department> depList = new ArrayList<department>();
department d1 = new department(100, "Sales","Business");
department d2 = new department(101, "Engineering","Technology");
department d3 = new department(102, "PR","Services");
depList.add(d1);
depList.add(d2);
depList.add(d3);
for(department b:depList){
System.out.println(b.deptCode+" "+b.parentDept+" "+b.deptName);
}
}
}
所以上面的代码工作正常并且显示正确。
我的人class
包 com.education;
public class person {
public int personId;
public String name;
private int deptCode;
private String parentDept;
private String deptName;
public person(int personId, String name, int deptCode, String parentDept, String deptName) {
super();
this.personId = personId;
this.name = name;
this.deptCode = deptCode;
this.parentDept = parentDept;
this.deptName = deptName;
}
public void display(){
System.out.println(" " + personId + " " +name + " " + deptCode+ " " + parentDept + " "+deptName);
System.out.println();
}
}
我的部门class
public class department {
private int deptCode;
private String parentDept;
private String deptName;
public department(int deptCode, String parentDept, String deptName) {
super();
this.deptCode = deptCode;
this.parentDept = parentDept;
this.deptName = deptName;
}
public void dispalyDepartment() {
System.out.println(" "+deptCode+" "+parentDept+" "+deptName);
}
}
现在我的目标是将 personDetails 和 departmentDetails 放入数组列表中并进行比较,然后根据部门代码找出差异。
这是我的逻辑:工作正常。
List<person> listC = new ArrayList<person>();
for(person p : list) {
boolean flag = false;
for (department d:depList) {
if(p.deptCode == d.deptCode) {
flag = false;
break;
}else {
flag = true;
}
}
if(flag == true) {
listC.add(p);
}
}
o/p应该是
(6,"Horward",103,"Leadership","Managmnet");
(7,"Cyna",104,"HR","Human Resource");
因为 deptCode : 103 和 104 不存在。
谁能帮帮我。我可以使用任何其他收集技术吗?
试试这个。
Set<Integer> deptCodes = Arrays.stream(departmentDetails)
.map(department -> department.deptCode)
.collect(Collectors.toSet());
List<person> persons = Arrays.stream(personDetails)
.filter(person -> !deptCodes.contains(person.deptCode))
.collect(Collectors.toList());
persons.forEach(person -> person.display());
输出:
6 Horward 103 Leadership Managmnet
7 Cyna 104 HR Human Resource