Rather than having 2 lists with roles assigned or unasigned, you should have only one list for a user with all roles. For each role you should have a boolean marker that marks whether this role is assigned to the user or not.
So to answer your question, you can just assign or unassign a role by just toggling the boolean between true and false.
But don't confuse the internal structure of the data with the view: the view can still show 2 lists.
For printing the first list(assigned roles), you go through the internal list and print all roles where the boolean is true. For printing the second list (unassigned roles) you go through the same list a second time, but print all roles where the boolean is false.
In struts, you can use the <logic:iterator> tag to print each list.
I bet you already have a preferred data structure (three dimensional array, hashmap of users and roles, classes with subclasses etc.)
But in case you don't have more than 64 roles, one thing to mention is a data structure that is quickest and smalles in memory: bitsets.
- public static final long ROLE1=1;
-
public static final long ROLE2=2;
-
public static final long ROLE3=4;
-
public static final long ROLE4=8;
-
public static final long ROLE5=16;
-
...
-
// set roles
-
long rightsUser1= ROLE1 | ROLE3; // Only role 1 and 3 are assigned
-
long rightsUser2= ROLE4; // Only role 4 is assigned
-
...
-
// add roles
-
rightsUser2 |= ROLE5; // role 5 is now added (assigned), no matter if it was assigned before or not.
-
...
-
// test roles
-
if ((rightsUser1 & ROLE2) != 0) out.println("User 1 has role 2 assigned");
-
...
-
// toggle role assignment
-
rightsUser2 ^= ROLE5;