Created
November 6, 2017 13:04
-
-
Save bmaggi/b009acc1c39e3c84f4c444eef2f403c4 to your computer and use it in GitHub Desktop.
A simple example to use predicate to filter data structure
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
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