Maslor
Maslor

Reputation: 1910

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. Thanks.

Upvotes: 1

Views: 107

Answers (3)

vefthym
vefthym

Reputation: 7462

Assuming that the Employee and the Manager classes both extend the User class, then yes, this is a good solution. If you want to prevent duplicates, however, you can use a 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();, 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:

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
    }
}

Upvotes: 3

Karthik
Karthik

Reputation: 5040

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.

If you want to prevent duplicates, use a Set, if you also want to maintain the order, use a 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
}

Upvotes: 1

Andrew Aitken
Andrew Aitken

Reputation: 681

You could use a Set instead of a List which would solve your duplicate problem, as for the iterator issue you'd have to add more information about where it's happening.

Upvotes: 1

Related Questions