Java 17 Recipes


-5. Filtering a Collection of Data


Download 3.2 Mb.
Pdf ko'rish
bet121/245
Sana02.06.2024
Hajmi3.2 Mb.
#1839910
1   ...   117   118   119   120   121   122   123   124   ...   245
Bog'liq
Java 17 Recipes

6-5. Filtering a Collection of Data
 Problem
You have a list of data to which you’d like to apply some filtering so that you can extract 
objects meeting the specified criteria.
 Solution
Create a stream from the list of data and apply a filter, passing the desired predicate
otherwise known as a conditional expression. Finally, add each object matching the 
specified filter criteria to a new list. In the following example, a list of Player objects is 
being filtered to capture only those players who have scored ten or more goals.
public static void main(String[] args){
loadTeam();
// Create Array to store the matching reaults
List
gteTenGoals = new ArrayList
();
team.stream().filter(
p -> p.getGoals() >= 10
&& p.getStatus() == 0)
.forEach(element -> gteTenGoals.add(element));
System.out.println("Number of Players Matching Criteria: " + gteTenGoals.
size());
Where the function loadTeam is:
public class Filter {
static List
team;
private static void loadTeam() {
System.out.println("Loading team...");
team = new ArrayList();
Player player1 = new Player();
player1.setFirstName("Josh");
player1.setLastName("Juneau");
player1.setGoals(5);
player1.setStatus(0);
ChapTer 6 LaMbda expressIons


224
Player player2 = new Player();
player2.setFirstName("Duke");
player2.setLastName("Java");
player2.setGoals(15);
player2.setStatus(0);
Player player3 = new Player();
player3.setFirstName("Jonathan");
player3.setLastName("Gennick");
player3.setGoals(1);
player3.setStatus(0);
Player player4 = new Player();
player4.setFirstName("Bob");
player4.setLastName("Smith");
player4.setGoals(18);
player4.setStatus(0);
Player player5 = new Player();
player5.setFirstName("Steve");
player5.setLastName("Adams");
player5.setGoals(7);
player5.setStatus(1);
team.add(player1);
team.add(player2);
team.add(player3);
team.add(player4);
team.add(player5);
}
 How It Works
The solution to this recipe makes use of a data stream since it contains an easy-to- 
use filter function. The collection of data, team, generates a stream, and then the filter 
function is called on it, accepting a predicate to filter the data within the collection. The 
predicate is written as a lambda expression that contains two such filtering criteria. The 
lambda expression passes a Player object as an argument and then filters the data based 
on the number of goals greater than or equal to ten and an active status.
ChapTer 6 LaMbda expressIons


225
Once the data has been filtered, the forEach() method adds each element that 
meets the filtering criteria to a list. This is also done using a lambda expression. The 
element added to the list is passed to the lambda expression as an argument and 
subsequently added to the list within the body of the expression.
Lambda expressions are very well suited for working within stream functions. Not 
only do they enable easier development of business logic, but they also make collections 
filtering easier to read and maintain.

Download 3.2 Mb.

Do'stlaringiz bilan baham:
1   ...   117   118   119   120   121   122   123   124   ...   245




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling