fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.function.Consumer;
  7. import java.util.stream.Collectors;
  8. import java.util.stream.IntStream;
  9.  
  10. /* Name of the class has to be "Main" only if the class is public. */
  11. class Ideone
  12. {
  13. public static void main (String[] args) throws java.lang.Exception
  14. {
  15. // 繰り返し処理に使うList
  16. List<Integer> list = IntStream.rangeClosed(1, 10).boxed().collect(Collectors.toList());
  17. myForEach(list,s -> System.out.print(s));
  18. System.out.println();
  19.  
  20. MyList<Integer> myList = new MyList<>(list);
  21. myList.myForEach(s -> System.out.print(s));
  22. }
  23.  
  24. private static <T> void myForEach(List<T> list, Consumer<T> consumer) {
  25. for (T t : list) {
  26. consumer.accept(t);
  27. }
  28. }
  29. }
  30.  
  31. class MyList<T> {
  32. List<T> list;
  33.  
  34. public MyList(List<T> list) {
  35. this.list = list;
  36. }
  37.  
  38. public void myForEach(Consumer<T> consumer){
  39. for (T t : list) {
  40. consumer.accept(t);
  41. }
  42. }
  43. }
Success #stdin #stdout 0.19s 2841600KB
stdin
Standard input is empty
stdout
12345678910
12345678910