如何从列表中按值获取课程?

时间:2014-12-17 20:34:06

标签: java

我有一个类,用于存储组对单个元素的权限。在其中,我还为单组的权限集上课。解决方案很简单,looks that

但我不知道如何使用其中的值设置单个权限(在group_id中)。我怎么能这样做?

public class PermissionsData {
    private List<PermissionsDataSingle> permissionsData;

    PermissionsData(List<PermissionsDataSingle> permissionsData) {
        this.permissionsData = permissionsData;
    }

    public PermissionsDataSingle getPermissionsByGroupID(int group_id) {
        // ToDo
    }

    public class PermissionsDataSingle {
        public int group_id;
        public boolean canView;
        public boolean canRead;
        public boolean canReply;
        public boolean canStart;
        public boolean canUpload;
        public boolean canDownload;
        PermissionsDataSingle(int group_id, boolean canView, boolean canRead, boolean canReply, boolean canStart, boolean canUpload, boolean canDownload) {
            this.group_id = group_id;
            this.canView = canView;
            this.canRead = canRead;
            this.canReply = canReply;
            this.canStart = canStart;
            this.canUpload = canUpload;
            this.canDownload = canDownload;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果列表总是非常短,您可以简单地遍历它并检查每个条目的group_id。否则最好使用地图。

for (PermissionsDataSingle pds : permissionsData) {
    if (pds.group_id == group_id) {
        return pds;
    }
}
// Not found