简体   繁体   中英

Check if java object exists in the Set

I have a Set and I will check if an Object with the same property still exists. My first approach was to iterate over the list and check but I guess there is a better approach in Java 8. Does anyone have any suggestion how to check this in an elegant way?

final Set<UserSelected> preparedUserSelected = new HashSet<>();

    UserSelected userSelected1 = new UserSelected();
    userSelected1.setInstitutionId("1");
    userSelected1.setUserId("1");
    userSelected1.setChatMessageFilter(ChatMessageFilterEnum.TYP2);

    UserSelected userSelected2 = new UserSelected();
    userSelected2.setInstitutionId("2");
    userSelected2.setUserId("2");
    userSelected2.setChatMessageFilter(ChatMessageFilterEnum.TYP1);

    UserSelected userSelected3 = new UserSelected();
    userSelected3.setInstitutionId("3");
    userSelected3.setUserId("3");
    userSelected3.setChatMessageFilter(ChatMessageFilterEnum.TYP2);

    preparedUserSelected.add(userSelected1);
    preparedUserSelected.add(userSelected2);
    preparedUserSelected.add(userSelected3);

    UserSelected userSelectedToCheck = new UserSelected();
    userSelectedToCheck.setInstitutionId("2");
    userSelectedToCheck.setUserId("2");
    userSelectedToCheck.setChatMessageFilter(ChatMessageFilterEnum.TYP1);

    boolean contains = false;
    for (final UserSelected userSelected : preparedUserSelected) {
        if (userSelectedToCheck.getInstitutionId().equals(userSelected.getInstitutionId())
            && userSelectedToCheck.getUserId().equals(userSelected.getUserId())
            && userSelectedToCheck.getChatMessageFilter() == userSelected.getChatMessageFilter())
            contains = true;
    }

    System.out.println("contains: " + contains);

You need to properly implement equals and hashCode methods in your UserSelected class. After that you can easily check the existence with preparedUserSelected.contains(userSelectedToCheck) .

Here's sample implementation of equals/hashCode for your class:

@Override
public int hashCode() {
    return Objects.hash(getUserId(), getInstitutionId(), getChatMessageFilter());
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null || getClass() != obj.getClass())
        return false;
    UserSelected other = (UserSelected) obj;
    return Objects.equals(getChatMessageFilter(), other.getChatMessageFilter()) &&
            Objects.equals(getInstitutionId(), other.getInstitutionId()) &&
            Objects.equals(getChatMessageFilter(), other.getChatMessageFilter());
}

Try this anyMatch of Lambda Expression(Java 8).

Returns whether any elements of this stream match the provided predicate. May not evaluate the predicate on all elements if not necessary for determining the result. If the stream is empty then false is returned and the predicate is not evaluated.

boolean isExist = preparedUserSelected.stream()
            .anyMatch(userSelected -> userSelected.getInstitutionId().equalsuserSelected.getInstitutionId());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM