Skip to content

Instantly share code, notes, and snippets.

@rain0r
Created February 15, 2018 12:22
Show Gist options
  • Save rain0r/2e3ed0dcde4a77db8fe3efc43e38315a to your computer and use it in GitHub Desktop.
Save rain0r/2e3ed0dcde4a77db8fe3efc43e38315a to your computer and use it in GitHub Desktop.
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
public class IntegerFilter {
public static void main(String... args) {
IntegerFilter rp = new IntegerFilter();
rp.doWork();
}
public void doWork() {
List<Integer> randoms = buildList();
randoms.stream().filter(onlyEvens()).forEach(
p -> {
System.out.println(String.format("%d is even", p));
});
}
private Predicate<Integer> onlyEvens() {
return result -> result.intValue() % 2 == 0;
}
private List<Integer> buildList() {
SecureRandom rand = new SecureRandom();
List<Integer> randoms = new ArrayList<>();
for (int i = 0; i < 5; i++) {
randoms.add(rand.nextInt());
}
return randoms;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment