diff --git a/core-java-collections-list/src/main/java/com/baeldung/collection/filtering/CollectionFilteringExamples.java b/core-java-collections-list/src/main/java/com/baeldung/collection/filtering/CollectionFilteringExamples.java deleted file mode 100644 index a7ab5840f7..0000000000 --- a/core-java-collections-list/src/main/java/com/baeldung/collection/filtering/CollectionFilteringExamples.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.baeldung.collection.filtering; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; - -/** - * Various filtering examples. - * - * @author Rodolfo Felipe - */ -public class CollectionFilteringExamples { - - private List buildEmployeeList() { - return Arrays.asList(new Employee(1, "Mike", 1), new Employee(2, "John", 1), new Employee(3, "Mary", 1), new Employee(4, "Joe", 2), new Employee(5, "Nicole", 2), new Employee(6, "Alice", 2), new Employee(7, "Bob", 3), new Employee(8, "Scarlett", 3)); - } - - private List employeeNameFilter() { - return Arrays.asList("Alice", "Mike", "Bob"); - } - - private List getFilteredEmployeeList() { - List filteredList = new ArrayList<>(); - for (Employee employee : buildEmployeeList()) { - for (String name : employeeNameFilter()) { - if (employee.getName() - .equalsIgnoreCase(name)) { - filteredList.add(employee); - } - } - } - return filteredList; - } - - private List getFilteredEmployeeListLambdaExample() { - return buildEmployeeList().stream() - .filter(employee -> employeeNameFilter().contains(employee.getName())) - .collect(Collectors.toList()); - } - - private List getFilteredEmployeeListLambdaExampleWithHashSetContains() { - Set nameFilterSet = employeeNameFilter().stream() - .collect(Collectors.toSet()); - return buildEmployeeList().stream() - .filter(employee -> nameFilterSet.contains(employee.getName())) - .collect(Collectors.toList()); - } - -} diff --git a/core-java-collections-list/src/test/java/com/baeldung/collection/filtering/CollectionFilteringTest.java b/core-java-collections-list/src/test/java/com/baeldung/collection/filtering/CollectionFilteringTest.java new file mode 100644 index 0000000000..bf3ec53abb --- /dev/null +++ b/core-java-collections-list/src/test/java/com/baeldung/collection/filtering/CollectionFilteringTest.java @@ -0,0 +1,73 @@ +package com.baeldung.collection.filtering; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +import org.hamcrest.Matchers; +import org.junit.Assert; +import org.junit.Test; + +/** + * Various filtering examples. + * + * @author Rodolfo Felipe + */ +public class CollectionFilteringTest { + + private List buildEmployeeList() { + return Arrays.asList(new Employee(1, "Mike", 1), new Employee(2, "John", 1), new Employee(3, "Mary", 1), new Employee(4, "Joe", 2), new Employee(5, "Nicole", 2), new Employee(6, "Alice", 2), new Employee(7, "Bob", 3), new Employee(8, "Scarlett", 3)); + } + + private List employeeNameFilter() { + return Arrays.asList("Alice", "Mike", "Bob"); + } + + @Test + public void givenEmployeeList_andNameFilterList_thenObtainFilteredEmployeeList_usingForEachLoop() { + List filteredList = new ArrayList<>(); + List originalList = buildEmployeeList(); + List nameFilter = employeeNameFilter(); + + for (Employee employee : originalList) { + for (String name : nameFilter) { + if (employee.getName() + .equalsIgnoreCase(name)) { + filteredList.add(employee); + } + } + } + + Assert.assertThat(filteredList.size(), Matchers.is(nameFilter.size())); + } + + @Test + public void givenEmployeeList_andNameFilterList_thenObtainFilteredEmployeeList_usingLambda() { + List filteredList; + List originalList = buildEmployeeList(); + List nameFilter = employeeNameFilter(); + + filteredList = originalList.stream() + .filter(employee -> nameFilter.contains(employee.getName())) + .collect(Collectors.toList()); + + Assert.assertThat(filteredList.size(), Matchers.is(nameFilter.size())); + } + + @Test + public void givenEmployeeList_andNameFilterList_thenObtainFilteredEmployeeList_usingLambdaAndHashSet() { + List filteredList; + List originalList = buildEmployeeList(); + Set nameFilterSet = employeeNameFilter().stream() + .collect(Collectors.toSet()); + + filteredList = originalList.stream() + .filter(employee -> nameFilterSet.contains(employee.getName())) + .collect(Collectors.toList()); + + Assert.assertThat(filteredList.size(), Matchers.is(nameFilterSet.size())); + } + +}