1- package findItems ;
2-
3- import static org .junit .Assert .assertEquals ;
4-
5- import java .text .ParseException ;
6- import java .util .ArrayList ;
7- import java .util .Collections ;
8- import java .util .List ;
9- import java .util .stream .Collectors ;
10-
11- import org .junit .Test ;
12-
13- public class FindItemsBasedOnValues {
14-
15- List <Employee > EmplList = new ArrayList <Employee >();
16- List <Department > deptList = new ArrayList <Department >();
17-
18- public static void main (String [] args ) throws ParseException {
19- FindItemsBasedOnValues findItems = new FindItemsBasedOnValues ();
20- findItems .givenDepartmentList_thenEmployeeListIsFilteredCorrectly ();
21- }
22-
23- @ Test
24- public void givenDepartmentList_thenEmployeeListIsFilteredCorrectly () {
25- Integer expectedId = 1002 ;
26-
27- populate (EmplList , deptList );
28-
29- List <Employee > filteredList = EmplList .stream ()
30- .filter (empl -> deptList .stream ()
31- .anyMatch (dept -> dept .getDepartment ()
32- .equals ("sales" ) && empl .getEmployeeId ()
33- .equals (dept .getEmployeeId ())))
34- .collect (Collectors .toList ());
35-
36- assertEquals (expectedId , filteredList .get (0 )
37- .getEmployeeId ());
38- }
39-
40- private void populate (List <Employee > EmplList , List <Department > deptList ) {
41- Employee employee1 = new Employee (1001 , "empl1" );
42- Employee employee2 = new Employee (1002 , "empl2" );
43- Employee employee3 = new Employee (1003 , "empl3" );
44-
45- Collections .addAll (EmplList , employee1 , employee2 , employee3 );
46-
47- Department department1 = new Department (1002 , "sales" );
48- Department department2 = new Department (1003 , "marketing" );
49- Department department3 = new Department (1004 , "sales" );
50-
51- Collections .addAll (deptList , department1 , department2 , department3 );
52- }
53- }
54-
55- class Employee {
56- Integer employeeId ;
57- String employeeName ;
58-
59- public Employee (Integer employeeId , String employeeName ) {
60- super ();
61- this .employeeId = employeeId ;
62- this .employeeName = employeeName ;
63- }
64-
65- public Integer getEmployeeId () {
66- return employeeId ;
67- }
68-
69- public String getEmployeeName () {
70- return employeeName ;
71- }
72-
73- }
74-
75- class Department {
76- Integer employeeId ;
77- String department ;
78-
79- public Department (Integer employeeId , String department ) {
80- super ();
81- this .employeeId = employeeId ;
82- this .department = department ;
83- }
84-
85- public Integer getEmployeeId () {
86- return employeeId ;
87- }
88-
89- public String getDepartment () {
90- return department ;
91- }
92-
1+ package com .baeldung .findItems ;
2+
3+ import static org .junit .Assert .assertEquals ;
4+
5+ import java .text .ParseException ;
6+ import java .util .ArrayList ;
7+ import java .util .Collections ;
8+ import java .util .List ;
9+ import java .util .stream .Collectors ;
10+
11+ import org .junit .Test ;
12+
13+ public class FindItemsBasedOnOtherStreamUnitTest {
14+
15+ private List <Employee > employeeList = new ArrayList <Employee >();
16+
17+ private List <Department > departmentList = new ArrayList <Department >();
18+
19+ @ Test
20+ public void givenDepartmentList_thenEmployeeListIsFilteredCorrectly () {
21+ Integer expectedId = 1002 ;
22+
23+ populate (employeeList , departmentList );
24+
25+ List <Employee > filteredList = employeeList .stream ()
26+ .filter (empl -> departmentList .stream ()
27+ .anyMatch (dept -> dept .getDepartment ()
28+ .equals ("sales" ) && empl .getEmployeeId ()
29+ .equals (dept .getEmployeeId ())))
30+ .collect (Collectors .toList ());
31+
32+ assertEquals (expectedId , filteredList .get (0 )
33+ .getEmployeeId ());
34+ }
35+
36+ private void populate (List <Employee > EmplList , List <Department > deptList ) {
37+ Employee employee1 = new Employee (1001 , "empl1" );
38+ Employee employee2 = new Employee (1002 , "empl2" );
39+ Employee employee3 = new Employee (1003 , "empl3" );
40+
41+ Collections .addAll (EmplList , employee1 , employee2 , employee3 );
42+
43+ Department department1 = new Department (1002 , "sales" );
44+ Department department2 = new Department (1003 , "marketing" );
45+ Department department3 = new Department (1004 , "sales" );
46+
47+ Collections .addAll (deptList , department1 , department2 , department3 );
48+ }
49+ }
50+
51+ class Employee {
52+ private Integer employeeId ;
53+ private String employeeName ;
54+
55+ Employee (Integer employeeId , String employeeName ) {
56+ super ();
57+ this .employeeId = employeeId ;
58+ this .employeeName = employeeName ;
59+ }
60+
61+ Integer getEmployeeId () {
62+ return employeeId ;
63+ }
64+
65+ public String getEmployeeName () {
66+ return employeeName ;
67+ }
68+
69+ }
70+
71+ class Department {
72+ private Integer employeeId ;
73+ private String department ;
74+
75+ Department (Integer employeeId , String department ) {
76+ super ();
77+ this .employeeId = employeeId ;
78+ this .department = department ;
79+ }
80+
81+ Integer getEmployeeId () {
82+ return employeeId ;
83+ }
84+
85+ String getDepartment () {
86+ return department ;
87+ }
88+
9389}
0 commit comments