如何以有效的方式与基于键的对象数组列表进行比较
How to compare to Arraylist of object based on key in efficient way
如何以高效的方式与基于键的对象数组列表进行比较。
我有一组对象 personDetails 和 departmentDetails。
这里我试图根据名为 deptCode 的属性找出两个对象之间的区别。
这是我正在尝试的。
package com.education;
import java.util.*;
import java.util.stream.Collectors;
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);
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);
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);
}
}
for(person b:listC){
System.out.println(b.personId+" "+b.name+" "+b.deptCode+" "+b.parentDept+" "+b.deptName);
}
}
}
现在打印我想要的东西。但我正在使用两个 for 循环。谁能帮我优化我的代码,因为我在这里使用了两个循环。我想尝试根据某些过滤掉增量的代码。
在 Collection 中我看到 set 可以但不能写那个。佳能帮我优化效率
输出:
6 Horward 103 Leadership Managmnet
7 Cyna 104 HR Human Resource
给定一个规模为 p
的人员列表和一个规模为 d
的部门列表,您当前的算法的复杂度为 O(p * d)
。
您可以将部门 迭代一次 并将他们的部门代码添加到 HashSet<Integer>
并检查每个人的部门代码集 - 复杂性:O(d + p)
.
这在计算上更高效,同时需要更多内存——经典的时间与内存权衡:
List<Person> persons = ...;
List<Department> departments = ...
Set<Integer> existingDepartmentIds = new HashSet<>();
for (Department d : departments) {
existingDepartmentIds.add(d.deptCode);
}
List<Person> personsWithMatchingDepartmentId = new ArrayList<>();
for (Person p : persons) {
if (existingDepartmentIds.contains(p.deptCode)) {
personsWithMatchingDepartmentId.add(p);
}
}
我以命令式的方式写了这篇文章,因为我不知道您是否精通 Stream
等。功能变体如下所示:
List<Person> persons = ...;
List<Department> departments = ...
Set<Integer> existingDepartmentIds = departments.stream()
.map(Department::getDeptCode)
.collect(Collectors.toSet());
List<Person> personsWithMatchingDepartmentId = persons.stream()
.filter(p -> existingDepartmentIds.contains(p.deptCode))
.collect(Collectors.toList());
roookeee 已经给出了很好的答案。我的方法大致相同,这是我的实现:
将所有部门添加到列表后,遍历部门,将代码添加到 HashSet。我不确定 class 方法到底是什么,但是,假设它们遵循 JavaBean 约定,它可能看起来像这样:
//Create an instance of HashSet
HashSet<Integer> codeSet = new HashSet<>();
//Iterate over the departments and add the department code to a set.
depList.forEach(dept->{
codeSet.add(dept.getDeptCode());
});
List<person> listC = new ArrayList<>();
list.forEach(p->{
if(!codeSet.contains(p.getDeptCode())){
listC.add(p);
}
});
/**
*This reads logically as, "For each person, if the set of department codes
*DOES NOT contain this persons department code, add them to the list".
*/
请原谅任何糟糕的代码格式,我有一段时间没说 Java 因为我一直在努力学习 Python。另外,我没有专业的编程经验,我只是一个自认为编码很整洁的少年,所以,不要把我的话当作福音。
如何以高效的方式与基于键的对象数组列表进行比较。
我有一组对象 personDetails 和 departmentDetails。
这里我试图根据名为 deptCode 的属性找出两个对象之间的区别。
这是我正在尝试的。
package com.education;
import java.util.*;
import java.util.stream.Collectors;
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);
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);
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);
}
}
for(person b:listC){
System.out.println(b.personId+" "+b.name+" "+b.deptCode+" "+b.parentDept+" "+b.deptName);
}
}
}
现在打印我想要的东西。但我正在使用两个 for 循环。谁能帮我优化我的代码,因为我在这里使用了两个循环。我想尝试根据某些过滤掉增量的代码。
在 Collection 中我看到 set 可以但不能写那个。佳能帮我优化效率
输出:
6 Horward 103 Leadership Managmnet
7 Cyna 104 HR Human Resource
给定一个规模为 p
的人员列表和一个规模为 d
的部门列表,您当前的算法的复杂度为 O(p * d)
。
您可以将部门 迭代一次 并将他们的部门代码添加到 HashSet<Integer>
并检查每个人的部门代码集 - 复杂性:O(d + p)
.
这在计算上更高效,同时需要更多内存——经典的时间与内存权衡:
List<Person> persons = ...;
List<Department> departments = ...
Set<Integer> existingDepartmentIds = new HashSet<>();
for (Department d : departments) {
existingDepartmentIds.add(d.deptCode);
}
List<Person> personsWithMatchingDepartmentId = new ArrayList<>();
for (Person p : persons) {
if (existingDepartmentIds.contains(p.deptCode)) {
personsWithMatchingDepartmentId.add(p);
}
}
我以命令式的方式写了这篇文章,因为我不知道您是否精通 Stream
等。功能变体如下所示:
List<Person> persons = ...;
List<Department> departments = ...
Set<Integer> existingDepartmentIds = departments.stream()
.map(Department::getDeptCode)
.collect(Collectors.toSet());
List<Person> personsWithMatchingDepartmentId = persons.stream()
.filter(p -> existingDepartmentIds.contains(p.deptCode))
.collect(Collectors.toList());
roookeee 已经给出了很好的答案。我的方法大致相同,这是我的实现:
将所有部门添加到列表后,遍历部门,将代码添加到 HashSet。我不确定 class 方法到底是什么,但是,假设它们遵循 JavaBean 约定,它可能看起来像这样:
//Create an instance of HashSet
HashSet<Integer> codeSet = new HashSet<>();
//Iterate over the departments and add the department code to a set.
depList.forEach(dept->{
codeSet.add(dept.getDeptCode());
});
List<person> listC = new ArrayList<>();
list.forEach(p->{
if(!codeSet.contains(p.getDeptCode())){
listC.add(p);
}
});
/**
*This reads logically as, "For each person, if the set of department codes
*DOES NOT contain this persons department code, add them to the list".
*/
请原谅任何糟糕的代码格式,我有一段时间没说 Java 因为我一直在努力学习 Python。另外,我没有专业的编程经验,我只是一个自认为编码很整洁的少年,所以,不要把我的话当作福音。