Java filter List that so it only contains objects that have same attribute as in another lists

Elias Johannes :

I got 2 lists containing several objects. I want to filter the objects that contain the same String value at a specific attribute. So let's say listA contains objects with attribute id. Same for listB, although it contains different objects. Some objects from both lists have the same id though. I want to filter these objects and put them in the a new list. This is what i got so far:

List<Customer> Clist = Customer.getAllCustomers();
    List<License> Llist = License.getAllLicenses();

    Predicate<Customer> customerNotNullPredicate = u -> (u.id != null);
    Predicate<License> licenseNotNullPredicate = u -> (u.id != null);

    List<Customer> Clistfiltered1 = Clist.parallelStream().filter(customerNotNullPredicate).collect(Collectors.toList());
    List<License> Llistfiltered1 = Llist.parallelStream().filter(licenseNotNullPredicate).collect(Collectors.toList());
    Clistfiltered1.retainAll(Llistfiltered1);
    try {
        Clistfiltered1.get(0);
    } catch (Exception e){
        System.out.println(e);
    }

If course, retainAll() doesn't return anything, as both lists just contain objects of the different type. How can i try to use retainAll() on a specific attribute of the objects?

Thank you a lot in advance.

Jose Zevallos :

in this case you can't use retainAll() , but I would solve the problem like this:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


public class DifferentCollections {

    public static void main(String[] args) {


        List<Customer> customers = new ArrayList<>(Arrays.asList(new Customer(1), new Customer(2), new Customer(10)));
        List<License> licenses = new ArrayList<>(Arrays.asList(new License(1), new License(2), new License(30)));

        List<Customer> filteredCustomers = customers.stream().
                filter(c -> customerIdFoundInLicensesList(c, licenses)).
                collect(Collectors.toList());

        System.out.println(filteredCustomers);
    }

    private static boolean customerIdFoundInLicensesList(Customer customer, List<License> licenses) {
        return licenses.stream().
                filter(l -> l.getId().equals(customer.getId())).
                findAny().
                isPresent();
    }
}

class Customer {
    Integer id;

    public Customer(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    @Override
    public String toString() {
        return "Customer{" + "id=" + id + '}';
    }
}

class License {
    Integer id;

    public License(Integer id) {
        this.id = id;
    }

    public Integer getId() {
        return id;
    }

    @Override
    public String toString() {
        return "License{" + "id=" + id + '}';
    }
}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=474988&siteId=1