Created
February 17, 2018 16:04
-
-
Save rakeshopensource/f5554be9e97ba85a8efa576f5978b7b8 to your computer and use it in GitHub Desktop.
Java8 Function and Predicate Example
This file contains hidden or 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.Consumer; | |
import java.util.function.Function; | |
import java.util.function.Predicate; | |
public class FunctionAndPredicate { | |
public static Predicate<String> startsWithA(final String letter) { | |
return name -> name.startsWith(letter); | |
} | |
public static void main(String[] args) { | |
final List<String> fruits = | |
Arrays.asList("Apple", "Banana", "Cherries", "Dates", "Eggfruit" | |
, "Fig", "Grapes", "Hackberry"); | |
final Function<String, Predicate<String>> startWithC = (String prefix) -> { | |
Predicate<String> checkWithLetter = (String name) -> name.startsWith(prefix); | |
return checkWithLetter; | |
}; | |
final Function<String, Predicate<String>> startWithD = (prefix) -> | |
(name) -> name.startsWith(prefix); | |
final Function<String, Predicate<String>> startsWithF = | |
new Function<String, Predicate<String>>() { | |
@Override | |
public Predicate<String> apply(String s) { | |
return new Predicate<String>() { | |
@Override | |
public boolean test(String name) { | |
return name.startsWith(s); | |
} | |
}; | |
} | |
}; | |
fruits.stream().filter(startsWithA("A")).forEach(System.out::println); | |
fruits.stream().filter(name -> name.startsWith("B")).forEach(System.out::println); | |
fruits.stream().filter(startWithC.apply("C")).forEach(System.out::println); | |
fruits.stream().filter(startWithD.apply("D")).forEach(System.out::println); | |
fruits.stream().filter(startsWithF.apply("F")).forEach(System.out::println); | |
fruits.stream().filter(startWithD.apply("G")).forEach(new Consumer<String>() { | |
public void accept(final String name) { | |
System.out.println(name); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Java8 Function and Predicate Example