如何将每个循环的scala更改为每个循环的java

如何将每个循环的scala更改为每个循环的java,java,scala,Java,Scala,此代码用于使用spring framework按角色对用户进行身份验证 由于特殊情况,这里的scala代码被更改为java val authentication: Authentication = SecurityContextHolder.getContext() .getAuthentication(); var flag = false; authentication.getAuthorities().foreach(authority => if (authorit

此代码用于使用spring framework按角色对用户进行身份验证

由于特殊情况,这里的scala代码被更改为java

val authentication: Authentication = SecurityContextHolder.getContext()
     .getAuthentication();

var flag = false;
authentication.getAuthorities().foreach(authority =>
  if (authority.getAuthority().equals(role)) {
    flag = true
});
我试过了,但还没有完成

public final Authentication authentication;
authentication=SecurityContextHolder.getContext().getAuthentication();
boolean flag=true;
authentication.getAuthorities();
for(Object authority:authentication){
    if (authority.getAuthority().equals(role)) {
        flag = true;
    }
}

我真的试过了,但没用。如果你知道这个,请在这里分享

在不知道authentication.getAuthories()是什么类型的情况下,下面是它的外观:

public final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
boolean flag = false;

for (Authority authority: authentication.getAuthorities()) {
    if (authority.getAuthority().equals(role)) flag = true;
}

您的java代码甚至不应该编译。(Spring security)
Authentication
不可
Iterable
,您正在尝试迭代它也是。(旁注)为什么要将代码更改为java?什么是需要这样做的“特殊条件”?顺便说一句,Scala版本应该是这样的:
val containsRole=authentication.getAuthorities().exists(authority=>authority==role)