Last active
August 29, 2015 14:13
-
-
Save JoaoGFarias/8d43318c0a33004b977d to your computer and use it in GitHub Desktop.
Collection filter using Java 8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ArrayList<String> owners = new ArrayList<String>(); | |
ArrayList<String> owner1Teams = new ArrayList<String>(); | |
//Creating the unfilter collection | |
owners.add("Owner 1 - Team 1"); | |
owners.add("Owner 1 - Team 2"); | |
owners.add("Owner 2 - Team 1"); | |
owners.add("Owner 2 - Team 2"); | |
//This will filter only the teams owned by Owner 1 | |
Predicate<String> ownerFilter = (String ownerName) -> ownerName.contains("Owner 1"); | |
//Actual use of the predicate, adding in owner1Teams collection the teams owned by Owner 1 | |
owners.stream().filter(ownerFilter).forEach(team -> owner1Teams.add(team)); | |
//Printing the results | |
for(String team : owner1Teams){ System.out.println(team); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment