How to split this into 3 groups?

Hi!

I am writing a code to compare supervisors and oldUsers and split them into newUsers, retainedUsers and deletedUsers.

Supervisors are the list of userIds which the person selects, oldUsers are a list of userIds which are already in the database.

I want to add new users into the database and delete the unwanted users.

Supervisors contains ids which may have old userIds, new userIds and deleted userIds.

Here is my comparison code:



        String[] supervisorIds = new String[]{"2", "4"};
        List<String> oldUsers = new ArrayList();
        oldUsers.add("1");
        oldUsers.add("2");
        oldUsers.add("3");
        List<Integer> newUserIds = new ArrayList<Integer>();
        List<Integer> retainedUserIds = new ArrayList<Integer>();
        List<Integer> deletedUserIds = new ArrayList<Integer>();
        if (!oldUsers.isEmpty()) {
            for (String supervisorId : supervisorIds) {
                for (String oldUser : oldUsers) {
                    if (!oldUser.equals(supervisorId) && !newUserIds.contains(Integer.parseInt(supervisorId))) {
                        newUserIds.add(Integer.parseInt(supervisorId));
                    }
                }
            }
            for (String supervisorId : supervisorIds) {
                for (String oldUser : oldUsers) {
                    if (oldUser.equals(supervisorId) && !retainedUserIds.contains(Integer.parseInt(supervisorId))) {
                        retainedUserIds.add(Integer.parseInt(supervisorId));
                    }
                }
            }
            int i = 0;
            List<Integer> delList = new ArrayList<Integer>();
            for (String oldUser : oldUsers) {
                for (String supervisorId : supervisorIds) {
                    if ((oldUser.getId() != Integer.parseInt(supervisorId)) && !deletedUserIds.contains(Integer.parseInt(supervisorId))) {
                        deletedUserIds.add(oldUser.getId());
                    } else {
                        if (newUserIds.contains(Integer.parseInt(supervisorId))) {
                            delList.add(Integer.parseInt(supervisorId));
                        }
                    }
                }
                i++;
            }
            for (Integer del : delList) {
                if (!retainedUserIds.contains(del)) {
                    newUserIds.remove(del);
                }
            }
        }


Problem is: I am getting only new users. I am not getting the correct retained users and deleted users.

Can someone suggest a better way of doing this, without too many for loops?

Thanks.

A: The code, as posted, does not compile.

B: You loop through all your oldIds for each supervisorId to determine if oldIds contains the supervisorId, why?

C: You loop through supervisorIds three times rather than handling the three conditions in one loop when you have a supervisorId ‘in hand’.

[INDENT]- Imagine you had (on paper) a list of names and three lists to check against, would you:
a: Read a name, see if it’s in the list of new users, then read the next name, checking new users again until you’re out of names, then start at the top of the name list again, now checking against old users.

  • or -
    b: Read a name from the list, see if it’s on any of your lists, then move on to the next name.
    [/INDENT]

D: Rather than doing all the parsing, why not pick one Object type, let’s say Integer, and make everything the same to ease comparisons? supervisorIds come in as an array of strings? Fine, convert them, once, at the start of the method to a List of Integers.

E: I’m confused by your test values. ‘old’ users are 1,2 & 3. The supervisor ids are 2 & 4. I’m guessing 2 should be retained, but is 4 a new user or one that should be deleted?

A: Sorry about that.

B: I did the loop to find out which supervisorIds are inside oldIds.

C: Sorry…I am not sure the best way to find out how to figure out which ids are in or not inside the list.

D: You are right.

E: old users: 1,2,3 and supervisors: 2 & 4. That means: 2 is retained. 1 & 3 should be deleted and 4 is new.

Can you give me some guidance? Thank you very much.

Okay, this comes down to Collections knowledge.

So, read through the method names of Collection and their summaries. Take your time, I’ll wait.

Ok, all done?

So, if Collection contains those methods, and ArrayList is a Collection…

I’ll write the pseudocode, you write the code.


for each supervisor
  if supervisor in old users
    move supervisor to retained users
  else 
    move supervisor to new users
end loop
remove all supervisors from old users
move old users to deleted users