tuntun wretee
tuntun wretee

Reputation: 49

filtering out the records of a set

I have a constructor which is define like this. Now I want to filter out some records as mentioned in below comments (in code) please advise on how to achieve this.

public class A 
{
    private HashSet<Integer> readPermissionGroup = new HashSet<Integer>();

    //constructor 
    A
    {
        this.readPermissionGroup.add(10);
        this.readPermissionGroup.add(11);
        this.readPermissionGroup.add(15);
        this.readPermissionGroup.add(16);
    }

    // ...
}

Now below is another piece of code which is doing some manipulation as shown below

Set<Group> groups = user.getGroups();
for (Group group : groups) {    
    //?? now here I want to filter out the records where g.id not in (10,11,15,15)
    //?? right now it is doing the opposite
    if (readPermissionGroup.contains(group.getId())) // i want to filter those record whose  
                                                     // value is not 
                                                     // 10,11,15,16
    {
        hasAccess = true;
        break;
    }
}
return hasAccess;

Upvotes: 1

Views: 60

Answers (1)

user180100
user180100

Reputation:

use a not ! in front of your condition or revert your boolean:

if (!readPermissionGroup.contains(group.getId()))

Upvotes: 2

Related Questions