简体   繁体   English

如何检查一组对象是否包含 object,该 object 的特定字段在 Java 中有特定值?

[英]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.

相关问题 如何检查 ArrayList 是否包含具有特定字段值的对象? - How can I check if an ArrayList contains an Object with a specific field value? 在Java中,如何检查集合是否包含特定类的实例? - In Java, how can I check if a collection contains an instance of a specific class? 如何从arrayList中删除特定对象,以及如何检查其中是否包含该对象? - How can I remove a specific object from an arrayList and how can I check if it contains this object? 我可以检查 List 是否包含一个对象,该对象具有在 java 中具有特定值的字段吗? - Can I check whether a List contains an object that has fields with a certain value in java? 如何使用Java流将一组对象转换为该对象的字段映射到一组字段? - How can I convert a Set of objects into a Map of the Object's fields to a set of fields using Java streams? 使用querydsl如何检查由一对多关系产生的一组对象中的特定对象? - Using querydsl how can I check against a specific object from a set of objects that result from a One to Many relationship? 如何检查数组或列表中对象的特定字段是否唯一? - How can I check if a specific fields of objects within an array or list are unique? 如何检查表上的特定TD是否包含我想要的值? - How can I check if the specific TD on a table contains the value i want? 如果 Set 包含具有某些字符串值的对象,如何检查 Java? - How to check in java if Set contains object with some string value? 如何发现对象是否包含设置为0的数字字段? - How can I discover if an object contains numeric fields that are set to 0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM