我想在rolePriority中找到最小值。我如何在for循环中获得最小编号。
UserRole.java
public class UserRole implements Serializable{
private Integer id;
private User user;
private Role role;
RoleAction.java
public class RoleAction implements Serializable{ private Integer id; private Role role; private Action action; }
for (UserRole userRole : userRoles) {
dbRoles.add(userRole.getRole().getRoleName());
int rolePriority=userRole.getRole().getPriority();
for (RoleAction ra: userRole.getRole().getRoleActions()) {
System.out.println("#########"+ra.getAction().getName());
System.out.println("@@@@@@@@@"+ra.getAction().getId());
map.put(ra.getAction().getName(), ra.getAction().getId());
}
}
如果要在任何用户角色中找到最低的角色优先级,则可以尝试以下操作:
int minPriority = -1;
for (UserRole userRole : userRoles) {
dbRoles.add(userRole.getRole().getRoleName());
int rolePriority = userRole.getRole().getPriority();
if (rolePriority < minPriority || minPriority == -1) {
minPriority = rolePriority;
}
for (RoleAction ra: userRole.getRole().getRoleActions()) {
System.out.println("#########"+ra.getAction().getName());
System.out.println("@@@@@@@@@"+ra.getAction().getId());
map.put(ra.getAction().getName(), ra.getAction().getId());
}
}
解释:
将该变量minPriority
初始化-1
为占位符值。在循环的每次迭代期间,将当前优先级(rolePriority
)与到目前为止(minPriority
)看到的最小值进行比较,并且可能会更新该最小值。同样,在循环的第一次迭代过程中,minPriority
将分配给rolePrioirity
。如果您的优先级可以使用该值-1
,则应将其初始化minPriority
为某个从未实际使用的值。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句