Skip to content

Instantly share code, notes, and snippets.

@JoaoGFarias
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoaoGFarias/8d43318c0a33004b977d to your computer and use it in GitHub Desktop.
Save JoaoGFarias/8d43318c0a33004b977d to your computer and use it in GitHub Desktop.
Collection filter using Java 8
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