Skip to content

Instantly share code, notes, and snippets.

@siwonred
Created August 7, 2018 04: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 siwonred/59d49b0b06c838a4e0d4e3297c4599f0 to your computer and use it in GitHub Desktop.
Save siwonred/59d49b0b06c838a4e0d4e3297c4599f0 to your computer and use it in GitHub Desktop.
Stream Problem
import java.util.ArrayList;
import org.apache.commons.lang3.BooleanUtils;
public class Test {
private boolean push(int i) {
System.out.println(String.format("push message: %d", i));
return i % 3 == 2;
}
public boolean run(int n) {
System.out.println(String.format("run for %d", n));
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < n; ++i) {
list.add(i);
}
return list.stream()
.map(this::push)
.filter(BooleanUtils::isTrue)
.findAny()
.orElse(false);
}
public static void main(String[] args) {
Test test = new Test();
test.run(1);
test.run(3);
test.run(5);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment