当前位置: 动力学知识库 > 问答 > 编程问答 >

java - How to get class from list by value therein?

问题描述:

I have class that stores groups' permissions for single element. In it, I've also class for single group's permissions set. The solution is simple, looks that.

But I have no idea how can I get single permissions set using value in it (in group_id). How can I do that?

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;

}

}

}

网友答案:

If the list is always very short, you can simply iterate through it and check the group_id for each entry. Otherwise it's better to use a map.

for (PermissionsDataSingle pds : permissionsData) {
    if (pds.group_id == group_id) {
        return pds;
    }
}
// Not found
分享给朋友:
您可能感兴趣的文章:
随机阅读: