Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

In this quick tutorial, we’ll learn how to find items from one list based on values from another list using Java 8 Streams.

2. Setting Up Entities

Let’s start with two entity classes – Employee and Department:

class Employee {
    Integer employeeId;
    String employeeName;

    // getters and setters
}

class Department {
    Integer employeeId;
    String department;

    // getters and setters
}

3. Filter for Objects Including Specific List of Values

The idea here is to filter a list of Employee objects based on a list of Department objects. More specifically, we want to find all Employees from a list that:

  • have “sales” as their department and
  • have a corresponding employeeId in a list of Departments

To achieve this, we filter one inside the other:

@Test
public void givenDepartmentList_thenEmployeeListIsFilteredCorrectly() {
    Integer expectedId = 1002;

    populate(emplList, deptList);

    List<Employee> filteredList = emplList.stream()
      .filter(empl -> deptList.stream()
        .anyMatch(dept -> 
          dept.getDepartment().equals("sales") && 
          empl.getEmployeeId().equals(dept.getEmployeeId())))
        .collect(Collectors.toList());

    assertEquals(1, filteredList.size());
    assertEquals(expectedId, filteredList.get(0)
      .getEmployeeId());
}

After populating both lists, we simply pass a Stream of Employee objects to the Stream of Department objects.

Next, to filter records based on our two conditions, we’re using the anyMatch predicate, inside which we have combined all the given conditions.

Finally, we collect the result into filteredList.

4. Filter for Objects Excluding Specific List of Values

Let’s continue with the same theme, but this time filter a list of Department objects based on a list of Employee objects to exclude. More specifically, we want to find all Departments from a list that exclude Employees corresponding to a list of employeeIds.

Further, these employeeIds exclude all employees from the “sales” department. To achieve this, we filter departments by excluding employee IDs from a second list employeeIdList:

@Test
    public void givenEmployeeListToExclude_thenDepartmentListIsFilteredCorrectly() {
        String expectedDepartment = "sales";

        List<Integer> employeeIdList = Arrays.asList(1001, 1002, 1004, 1005);

        populate(employeeList, departmentList);
      
        List<Department> filteredList = departmentList.stream()
          .filter(dept -> !employeeIdList.contains(dept.getEmployeeId()))
          .collect(Collectors.toList());

        assertNotEquals(expectedDepartment, department.getDepartment());
    }

After populating both lists, we create a Stream of Department objects and filter records based on the employeeIdList to exclude.

Finally, we collect the result into filteredList.

5. Conclusion

In this article, we learned how to:

  • stream values of one list into the other list using Collection#stream and
  • combine multiple filter conditions using the anyMatch() predicate
  • discuss examples to include or exclude objects in one list based on objects in a second list
The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 4th December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW