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+
93+ }
0 commit comments