Skip to content

Instantly share code, notes, and snippets.

@bmaggi
Created November 6, 2017 13:04
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 bmaggi/b009acc1c39e3c84f4c444eef2f403c4 to your computer and use it in GitHub Desktop.
Save bmaggi/b009acc1c39e3c84f4c444eef2f403c4 to your computer and use it in GitHub Desktop.
A simple example to use predicate to filter data structure
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;
/**
* A simple example to use predicate to filter data structure
* @author Benoit Maggi
*/
public class PredicateFilter {
public static final String[] dictionary = {"a", "ab", "ba","c"};
public static List<String> getMatchingWords(Predicate<String> p){
return (List<String>) Arrays.stream(dictionary).filter(p).collect(Collectors.toList());
}
public static void main(String[] args) {
List<String> startWithA = getMatchingWords( new Predicate<String>() {
@Override
public boolean test(String t) {
return t.startsWith("a");
}
});
List<String> containsA = getMatchingWords( new Predicate<String>() {
@Override
public boolean test(String t) {
return t.contains("a");
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment