fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.function.*;
  5. import java.util.stream.*;
  6. import java.lang.*;
  7. import java.io.*;
  8.  
  9.  
  10. class Ideone
  11. {
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14.  
  15. PredicateFactory predicateFactory = new PredicateFactoryImpl();
  16. FilterFactory filterFactory = new FilterFactoryImpl();
  17. StreamFactory streamFactory = new StreamFactoryImpl();
  18.  
  19. streamFactory.create()
  20. .stream()
  21. .map(filterFactory.create())
  22. .filter(predicateFactory.create())
  23. .collect(Collectors.toList())
  24. .forEach(System.out::println);
  25. ;
  26. }
  27.  
  28.  
  29. interface PredicateFactory{
  30. Predicate<Integer> create();
  31. }
  32. static class PredicateFactoryImpl implements PredicateFactory{
  33. @Override
  34. public Predicate<Integer> create() {
  35. return new Predicate<Integer>() {
  36. @Override
  37. public boolean test(Integer x) {
  38. return x > 0 && x < 6;
  39. }
  40. };
  41. }
  42. }
  43.  
  44. interface FilterFactory{
  45. Function<Integer,Integer> create();
  46. }
  47. static class FilterFactoryImpl implements FilterFactory{
  48. @Override
  49. public Function<Integer,Integer> create() {
  50. return new Function<Integer,Integer> (){
  51. @Override
  52. public Integer apply(Integer x) {
  53. return x%10;
  54. }
  55. };
  56. }
  57. }
  58.  
  59. interface StreamFactory{
  60. List<Integer> create();
  61. }
  62. static class StreamFactoryImpl implements StreamFactory{
  63. @Override
  64. public List<Integer> create() {
  65. return Arrays.asList(0, 1, -2, -3, -4, 5,-6, -7 , -8, -9, 10, -11, 12, 14, 13);
  66. }
  67. }
  68. }
Success #stdin #stdout 0.1s 48464KB
stdin
Standard input is empty
stdout
1
5
2
4
3