1

我有一个这样定义的构造函数。现在我想过滤掉下面评论中提到的一些记录(在代码中),请告知如何实现这一点。

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

    // ...
}

现在下面是另一段代码,它正在做一些操作,如下所示

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;
4

1 回答 1

2

!在您的条件前面使用 not或恢复您的布尔值:

if (!readPermissionGroup.contains(group.getId()))
于 2013-09-11T05:14:21.370 回答