- 失败操作
List<RoleOperation> roleOperations = roleOperationRepository.findRoleOperationsByRoleIdIn(role_ids);
List<OperationPK> operationPKList = new ArrayList<>();
for (int i = 0; i < roleOperations.size(); i++) {
operationPKList.add(new OperationPK(roleOperations.get(i).getMenuId(), roleOperations.get(i).getRoleId()));
}
operationPKList = operationPKList.stream().distinct().collect(Collectors.toList());
List<Operation> operationList = operationRepository.findAllById(operationPKList);
String[] authorities = new String[operationList.size()];
for (int i = 0; i < operationList.size(); i++) {
authorities[i] = operationList.get(i).getOperationName();
}
失败原因分析:对于类的去重,虽然类属性值相等,但是在去重的时候比较的是地址,所以对于类的去重一定要在类中重写equals和hashcode方法。
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
OperationPK that = (OperationPK) o;
return menuId == that.menuId &&
operationId == that.operationId;
}
@Override
public int hashCode() {
return Objects.hash(menuId, operationId);
}
去重成功