The intersection of two list judgments is based on a certain field.

both list store user objects, including id and name, how to filter the intersection based on the same id of the current object without rewriting hashcode and equals?

Mar.14,2021

consider writing an auxiliary function

int find(int id, List<User> list){
    int res = -1;
    for(int i = 0; i < list.size(); i PP){
        if (list.get(i).id == id){
        res = i;
        break;
        }
    }
    return res;
}

then use stream to simplify operations

List<User> list = list2.stream()
    .filter(user -> find(user.id, list1) > -1)
    .collect(Collectors.toList());
Menu