From 7e2de11a5afaee971e4e54cf05540217263c1ff5 Mon Sep 17 00:00:00 2001 From: Rodolfo Felipe Date: Mon, 4 Mar 2019 00:36:23 -0400 Subject: [PATCH 1/4] BAEL-2448 Code examples for article. --- .../CollectionFilteringExamples.java | 51 +++++++++++++++++++ .../collection/filtering/Employee.java | 44 ++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 core-java-collections-list/src/main/java/com/baeldung/collection/filtering/CollectionFilteringExamples.java create mode 100644 core-java-collections-list/src/main/java/com/baeldung/collection/filtering/Employee.java 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 new file mode 100644 index 0000000000..a7ab5840f7 --- /dev/null +++ b/core-java-collections-list/src/main/java/com/baeldung/collection/filtering/CollectionFilteringExamples.java @@ -0,0 +1,51 @@ +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/main/java/com/baeldung/collection/filtering/Employee.java b/core-java-collections-list/src/main/java/com/baeldung/collection/filtering/Employee.java new file mode 100644 index 0000000000..2aa267b4dc --- /dev/null +++ b/core-java-collections-list/src/main/java/com/baeldung/collection/filtering/Employee.java @@ -0,0 +1,44 @@ +package com.baeldung.collection.filtering; + +/** + * Java 8 Collection Filtering by List of Values base class. + * + * @author Rodolfo Felipe + */ +public class Employee { + + private Integer employeeNumber; + private String name; + private Integer departmentId; + + public Employee(Integer employeeNumber, String name, Integer departmentId) { + this.employeeNumber = employeeNumber; + this.name = name; + this.departmentId = departmentId; + } + + public Integer getEmployeeNumber() { + return employeeNumber; + } + + public void setEmployeeNumber(Integer employeeNumber) { + this.employeeNumber = employeeNumber; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Integer getDepartmentId() { + return departmentId; + } + + public void setDepartmentId(Integer departmentId) { + this.departmentId = departmentId; + } + +} From 3186e484037e39ce9fa61874ce911f8a9dd90156 Mon Sep 17 00:00:00 2001 From: Rodolfo Felipe Date: Mon, 4 Mar 2019 20:43:14 -0400 Subject: [PATCH 2/4] BAEL-2448-Rev1 Changing examples to unit tests instead of plain methods after advice from editor. --- .../CollectionFilteringExamples.java | 51 ------------- .../filtering/CollectionFilteringTest.java | 73 +++++++++++++++++++ 2 files changed, 73 insertions(+), 51 deletions(-) delete mode 100644 core-java-collections-list/src/main/java/com/baeldung/collection/filtering/CollectionFilteringExamples.java create mode 100644 core-java-collections-list/src/test/java/com/baeldung/collection/filtering/CollectionFilteringTest.java 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())); + } + +} From bf31484ee367db29fc23a190698da2207ce6d0de Mon Sep 17 00:00:00 2001 From: Rodolfo Felipe Date: Mon, 4 Mar 2019 20:49:30 -0400 Subject: [PATCH 3/4] BAEL-2448-Rev2 Changing method names to comply with Jenkins PMD method naming rules. --- .../collection/filtering/CollectionFilteringTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 index bf3ec53abb..3b4a087ecd 100644 --- 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 @@ -26,7 +26,7 @@ public class CollectionFilteringTest { } @Test - public void givenEmployeeList_andNameFilterList_thenObtainFilteredEmployeeList_usingForEachLoop() { + public void givenEmployeeList_andNameFilterList_thenObtainFilteredEmployeeList_usingForEachLoopUnitTest() { List filteredList = new ArrayList<>(); List originalList = buildEmployeeList(); List nameFilter = employeeNameFilter(); @@ -44,7 +44,7 @@ public class CollectionFilteringTest { } @Test - public void givenEmployeeList_andNameFilterList_thenObtainFilteredEmployeeList_usingLambda() { + public void givenEmployeeList_andNameFilterList_thenObtainFilteredEmployeeList_usingLambdaUnitTest() { List filteredList; List originalList = buildEmployeeList(); List nameFilter = employeeNameFilter(); @@ -57,7 +57,7 @@ public class CollectionFilteringTest { } @Test - public void givenEmployeeList_andNameFilterList_thenObtainFilteredEmployeeList_usingLambdaAndHashSet() { + public void givenEmployeeList_andNameFilterList_thenObtainFilteredEmployeeList_usingLambdaAndHashSetUnitTest() { List filteredList; List originalList = buildEmployeeList(); Set nameFilterSet = employeeNameFilter().stream() From 245e0f32b33ed5c3465bead17bbf87a8cb49d8a2 Mon Sep 17 00:00:00 2001 From: Rodolfo Felipe Date: Mon, 4 Mar 2019 20:54:50 -0400 Subject: [PATCH 4/4] BAEL-2448-Rev3 Renaming test class so it passes PMD checks. Reverting method names to previous commit. --- ...ilteringTest.java => CollectionFilteringUnitTest.java} | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) rename core-java-collections-list/src/test/java/com/baeldung/collection/filtering/{CollectionFilteringTest.java => CollectionFilteringUnitTest.java} (92%) 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/CollectionFilteringUnitTest.java similarity index 92% rename from core-java-collections-list/src/test/java/com/baeldung/collection/filtering/CollectionFilteringTest.java rename to core-java-collections-list/src/test/java/com/baeldung/collection/filtering/CollectionFilteringUnitTest.java index 3b4a087ecd..cbc7136192 100644 --- 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/CollectionFilteringUnitTest.java @@ -15,7 +15,7 @@ import org.junit.Test; * * @author Rodolfo Felipe */ -public class CollectionFilteringTest { +public class CollectionFilteringUnitTest { 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)); @@ -26,7 +26,7 @@ public class CollectionFilteringTest { } @Test - public void givenEmployeeList_andNameFilterList_thenObtainFilteredEmployeeList_usingForEachLoopUnitTest() { + public void givenEmployeeList_andNameFilterList_thenObtainFilteredEmployeeList_usingForEachLoop() { List filteredList = new ArrayList<>(); List originalList = buildEmployeeList(); List nameFilter = employeeNameFilter(); @@ -44,7 +44,7 @@ public class CollectionFilteringTest { } @Test - public void givenEmployeeList_andNameFilterList_thenObtainFilteredEmployeeList_usingLambdaUnitTest() { + public void givenEmployeeList_andNameFilterList_thenObtainFilteredEmployeeList_usingLambda() { List filteredList; List originalList = buildEmployeeList(); List nameFilter = employeeNameFilter(); @@ -57,7 +57,7 @@ public class CollectionFilteringTest { } @Test - public void givenEmployeeList_andNameFilterList_thenObtainFilteredEmployeeList_usingLambdaAndHashSetUnitTest() { + public void givenEmployeeList_andNameFilterList_thenObtainFilteredEmployeeList_usingLambdaAndHashSet() { List filteredList; List originalList = buildEmployeeList(); Set nameFilterSet = employeeNameFilter().stream()