gpt4 book ai didi

java - 过滤掉集合中的记录

转载 作者:行者123 更新时间:2023-11-30 04:14:34 26 4
gpt4 key购买 nike

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

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;

最佳答案

在条件前面使用 not ! 或恢复 boolean 值:

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

关于java - 过滤掉集合中的记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18733098/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com