How to perform join query using Java stream

I am fetching data from db and storing it in list. Then based on the the id, I want to create new Object. I have already implemented it using old for loop method. I am trying to implement it using java stream. Any help? Thanks.

        List<Employee> emp = new ArrayList<Employee>();
        emp.add(new Employee(1, "Ben", "Glasgow"));
        emp.add(new Employee(2, "Max", "Seattle"));
        emp.add(new Employee(3, "Sam", "Mumbai"));
        emp.add(new Employee(4, "John", "Aukland"));
        emp.add(new Employee(5, "Rob", "Tokyo"));

        List<Department> dpt = new ArrayList<Department>();
        dpt.add(new Department(1, 40000, "tech"));
        dpt.add(new Department(2,  30000, "mgm"));
        dpt.add(new Department(3,  50000, "tech"));
        dpt.add(new Department(4,  30000, "mgm"));


        List<EmpDep> empDep = new ArrayList<EmpDep>();
        
        /**How to do this using stream
        for (int i = 0; i < emp.size(); i++){
            empDep.add(new EmpDep(emp.get(i).getId(), emp.get(i).getName(), dpt.get(i).getSalary()));
        }
        */


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation