Skip to content

Commit 7e2de11

Browse files
committed
BAEL-2448
Code examples for article.
1 parent 8f7ebdb commit 7e2de11

2 files changed

Lines changed: 95 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.baeldung.collection.filtering;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
import java.util.Set;
7+
import java.util.stream.Collectors;
8+
9+
/**
10+
* Various filtering examples.
11+
*
12+
* @author Rodolfo Felipe
13+
*/
14+
public class CollectionFilteringExamples {
15+
16+
private List<Employee> buildEmployeeList() {
17+
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));
18+
}
19+
20+
private List<String> employeeNameFilter() {
21+
return Arrays.asList("Alice", "Mike", "Bob");
22+
}
23+
24+
private List<Employee> getFilteredEmployeeList() {
25+
List<Employee> filteredList = new ArrayList<>();
26+
for (Employee employee : buildEmployeeList()) {
27+
for (String name : employeeNameFilter()) {
28+
if (employee.getName()
29+
.equalsIgnoreCase(name)) {
30+
filteredList.add(employee);
31+
}
32+
}
33+
}
34+
return filteredList;
35+
}
36+
37+
private List<Employee> getFilteredEmployeeListLambdaExample() {
38+
return buildEmployeeList().stream()
39+
.filter(employee -> employeeNameFilter().contains(employee.getName()))
40+
.collect(Collectors.toList());
41+
}
42+
43+
private List<Employee> getFilteredEmployeeListLambdaExampleWithHashSetContains() {
44+
Set<String> nameFilterSet = employeeNameFilter().stream()
45+
.collect(Collectors.toSet());
46+
return buildEmployeeList().stream()
47+
.filter(employee -> nameFilterSet.contains(employee.getName()))
48+
.collect(Collectors.toList());
49+
}
50+
51+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.collection.filtering;
2+
3+
/**
4+
* Java 8 Collection Filtering by List of Values base class.
5+
*
6+
* @author Rodolfo Felipe
7+
*/
8+
public class Employee {
9+
10+
private Integer employeeNumber;
11+
private String name;
12+
private Integer departmentId;
13+
14+
public Employee(Integer employeeNumber, String name, Integer departmentId) {
15+
this.employeeNumber = employeeNumber;
16+
this.name = name;
17+
this.departmentId = departmentId;
18+
}
19+
20+
public Integer getEmployeeNumber() {
21+
return employeeNumber;
22+
}
23+
24+
public void setEmployeeNumber(Integer employeeNumber) {
25+
this.employeeNumber = employeeNumber;
26+
}
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
36+
public Integer getDepartmentId() {
37+
return departmentId;
38+
}
39+
40+
public void setDepartmentId(Integer departmentId) {
41+
this.departmentId = departmentId;
42+
}
43+
44+
}

0 commit comments

Comments
 (0)