Why the java stream break the outer loop if there is a match in the inner loop ! I mean it break from the outer loop
public class Employee {
private long id;
private Long nin;
}
public class Iban {
private long id;
private Long nin;
}
and I have the following data:
Employee e1= new Employee();
e1.setId(1);
e1.setNin(111111l);
Employee e2= new Employee();
e2.setId(2);
e2.setNin(222222l);
Employee e3= new Employee();
e3.setId(3);
e3.setNin(333333l);
List<Employee> employees = new ArrayList<>();
employees.add(e1);
employees.add(e2);
employees.add(e3);
Iban b1= new Iban();
b1.setId(1);
b1.setNin(111111l);
Iban b3= new Iban();
b3.setId(3);
b3.setNin(333333l);
List<Iban> ibans = new ArrayList<>();
ibans.add(b1);
ibans.add(b3);
i found that looping using java streams will print only one match while using normal loops will find two matches as below
// this will note iterate on all employees!
for(Employee emp: employees){
Optional<Iban> matchingContact = ibans.stream()
.findAny()
.filter(iban -> iban.getNin().equals(emp.getNin()));
if (matchingContact.isPresent()) {
System.out.println("***");
}
}
// while this find two matches and will iterate over all employees
for(Employee emp: employees){
for(Iban ib: ibans){
if(ib.getNin().equals(emp.getNin())){
System.out.println("***");
break;
}
}
}
and it is expeted to find two matches !