[英]How can I check if a Set of objects contains an object having a specific fields with a specific value in Java?
I am working on a Spring Boot application and I am not so confidend with the new Java 8 features.我正在开发一个 Spring 引导应用程序,我对新的 Java 8 功能不太有信心。 I have the following problem.我有以下问题。
In my code I have this service method:在我的代码中,我有这个服务方法:
@Override
public List<User> getClientsOfASubAgentUser(int subAgentId) throws NotFoundException {
Optional<User> retrievedUser = this.userRepository.findById(subAgentId);
if(retrievedUser.isEmpty()) {
throw new NotFoundException(String.format("The SUB-AGENT user having ID or email %s was not found", Integer.toString(subAgentId)));
}
// CHECK IF THE Set<UserType> returned by the getUserTypes() method contain an object having "typeName" field value equals to "SUB-AGENT"
if(retrievedUser.get().getUserTypes().????)
List<User> clientsList = this.userRepository.findByParent_Id(subAgentId);
return clientsList;
}
My problem is on this line (that is incomplete):我的问题出在这一行(不完整):
// CHECK IF THE Set<UserType> returned by the getUserTypes() method contain an object having "typeName" field value equals to "SUB-AGENT"
if(retrievedUser.get().getUserTypes().????)
The getUserTypes() return a Set . getUserTypes()返回一个Set 。 This is the UserType entity class:这是UserType实体 class:
@Entity
@Table(name = "user_type")
@Getter
@Setter
public class UserType implements Serializable {
private static final long serialVersionUID = 6904959949570501298L;
@Id
@Column(name = "id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name = "type_name")
private String typeName;
@Column(name = "description")
private String description;
@ManyToMany(cascade = { CascadeType.MERGE })
@JoinTable(
name = "user_type_operation",
joinColumns = { @JoinColumn(name = "fk_user_type_id") },
inverseJoinColumns = { @JoinColumn(name = "fk_operation_id") }
)
Set<Operation> operations;
public UserType() {
super();
// TODO Auto-generated constructor stub
}
public UserType(String typeName, String description) {
super();
this.typeName = typeName;
this.description = description;
}
}
Basicallu I need to find a smart way (maybe using lambda function?) to check if into the Set returned by the getUserTypes() there is a UserType object having the typeName field equals to the SUB-AGENT string. Basicallu 我需要找到一个聪明的方法(也许使用 lambda function?)来检查getUserTypes()返回的Set中是否有一个 UserType ZA8CFDE6331BD59EB266666ZF8911C4等于字符串B
How can I correctly and nicely implement this behavior?我怎样才能正确和很好地实现这种行为?
You can use anyMatch
for this.您可以为此使用anyMatch
。 It is a terminal operation on the stream which returns true if the passed predicate (condition) evaluates to true for at least one element (UserType instance).这是 stream 上的终端操作,如果传递的谓词(条件)对于至少一个元素(UserType 实例)的计算结果为true ,则返回true 。 It is a short-circuiting operation.这是一个短路操作。
boolean hasTypeName = retrievedUser.get().getUserTypes()
.stream()
.anyMatch(userType -> userType.getTypeName.equals("SUB-AGENT")
if (hasTypeName) {...}
or inline it as或将其内联为
if (retrievedUser.get().getUserTypes()
.stream()
.anyMatch(userType -> userType.getTypeName.equals("SUB-AGENT")) {...}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.