[英]How do I add two ArrayLists of different classes that extend an abstract class into one ArrayList?
So I have the two following arraylists: 所以我有以下两个数组列表:
private List<Manager> _managers = new ArrayList<Manager>(); //Manager extends User
private List<Employee> _employees = new ArrayList<Employee>(); //Employee extends User
I want to have an arraylist that combines both of these, storing all users, both employees and managers. 我想要一个将两者结合起来的数组列表,存储所有用户,包括员工和经理。
Currently I am trying the following solution: 目前,我正在尝试以下解决方案:
private List<User> _usrs = new ArrayList<User>();
/*
* function that I use before I get the current user list, preventing getting a outdated version of it
*/
public void refreshUserList() {
this._usrs.clear(); //prevent duplicates
this._usrs.addAll(_employees); //add all employees to user list
this._usrs.addAll(_managers); //add all managers to user list
}
Is this a good way to solve this problem? 这是解决这个问题的好方法吗? I'm having some nullPointer iterator issues when iterating this list and before I get into solving this issue, I'd like to know if the problem isn't right here. 在迭代此列表时,我遇到了一些nullPointer迭代器问题,在开始解决此问题之前,我想知道问题是否就在这里。 Thanks. 谢谢。
Assuming that the Employee
and the Manager
classes both extend the User
class, then yes, this is a good solution. 假设Employee
和Manager
类都扩展了User
类,那么是的,这是一个很好的解决方案。 If you want to prevent duplicates, however, you can use a Set: 但是,如果要防止重复,可以使用Set:
private Set<User> _usrs = new HashSet<>();
/*
* function that I use before I get the current user set, preventing getting a outdated version of it
*/
public void refreshUserList() {
this._usrs.addAll(_employees); //add all employees to user set
this._usrs.addAll(_managers); //add all managers to user set
}
You don't need to call clear();
您无需调用clear();
, since Sets do not add duplicate elements. ,因为集合不会添加重复的元素。
This way, you will lose the special variables/methods that appear only for Managers or Employees, since all you will have will be the variables/methods available for Users. 这样,您将丢失仅对经理或雇员显示的特殊变量/方法,因为您所拥有的全部将是用户可用的变量/方法。 However, if you cast each User
instance of your _usrs
Set to Manager
or Employee
, then you can still get those special elements/methods: 但是,如果将_usrs
Set的每个User
实例_usrs
为Manager
或Employee
,那么您仍然可以获得这些特殊元素/方法:
for (User user : _usrs) {
if (user instanceof Manager) {
Manager mng = (Manager) user;
mng.manage(); // or whatever managers do that employees don't
} else { //if no other classes extend the User class
Employee emp = (Employee) user;
emp.work(); // or whatever employees do that managers don't :P
}
}
您可以使用Set而不是List来解决重复的问题,因为对于迭代器问题,您必须添加有关发生位置的更多信息。
I am guessing that your Manager
and Employee
classes are extending/ implementing your User
class, Only then you will be able to combine the two arraylists. 我猜想您的Manager
和Employee
类正在扩展/实现您的User
类,只有这样您才可以组合两个数组列表。
If you want to prevent duplicates, use a Set , if you also want to maintain the order, use a LinkedHashSet . 如果要防止重复,请使用Set ;如果还要维护顺序,请使用LinkedHashSet 。
private LinkedHashSet<User> _usrs = new LinkedHashSet<User>();
public void refreshUserList() {
usrs.clear(); //prevent duplicates
usrs.addAll(_employees); //add all employees to user list
usrs.addAll(_managers); //add all managers to user list
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.