I have two lists of objects localList and remotList, both lists have a consent attribute
I want to check the value of the consent attribute if the same for both lists
If Not, I want to remove all objects from localList don't have same consent value that exists in remotList and add all objects have same consent value that exist in remotList and not in localList
I implemented this solution but I want to improve it
Java Example
class Customer{
private Long id;
private String name;
private Integer consent;
public Customer(Long id, String name, Integer consent) {
this.id = id;
this.name = name;
this.consent = consent;
}
public Integer getConsent() {
return consent;
}
@java.lang.Override
public java.lang.String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", consent=" + consent +
'}';
}
}
public static void main(String[] args) {
List<Customer> localList = new ArrayList<>();
localList.add(new Customer(1L, "name1", 12));
localList.add(new Customer(2L, "name2", 13));
localList.add(new Customer(3L, "name3", 14));
localList.add(new Customer(4L, "name4", 15));
List<Customer> remoteList = new ArrayList<>();
remoteList.add(new Customer(10L, "name1", 12));
remoteList.add(new Customer(11L, "name2", 11));
remoteList.add(new Customer(12L, "name3", 14));
remoteList.add(new Customer(13L, "name4", 16));
Map<Integer, Customer> map = remoteList.stream()
.collect(Collectors.toMap(s -> s.getConsent() , s -> s));
Map<Integer, Customer> map2 = localList.stream()
.collect(Collectors.toMap(s -> s.getConsent() , s -> s));
List<Customer> remove = new ArrayList<>();
localList.forEach(e -> {
if(map.get(e.getConsent()) == null ) {
remoteList.add(e);
}
});
remoteList.forEach(e -> {
if(map2.get(e.getConsent()) == null ) {
remove.add(e);
}
});
remove.forEach(e -> remoteList.remove(e));
remoteList.forEach(System.out::println);
the remoteList
Customer{id=10, name='name1', consent=12}
Customer{id=11, name='name2', consent=11}
Customer{id=12, name='name3', consent=14}
Customer{id=13, name='name4', consent=16}
the localList
Customer{id=1, name='name1', consent=12}
Customer{id=2, name='name2', consent=13}
Customer{id=3, name='name3', consent=14}
Customer{id=4, name='name4', consent=15}
Result
Customer{id=10, name='name1', consent=12}
Customer{id=12, name='name3', consent=14}
Customer{id=2, name='name2', consent=13}
Customer{id=4, name='name4', consent=15}