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.stream.*;
  7. import static java.util.Spliterator.*;
  8.  
  9. class Ideone
  10. {
  11. public static class Hoge implements Iterator<List<Integer>> {
  12. final Iterator<Integer> it;
  13. public Hoge(Iterator<Integer> it) {
  14. this.it=it;
  15. }
  16. private List<Integer> nextValue=null;
  17. private boolean isEof = false;
  18. @Override
  19. public boolean hasNext() {
  20. if (!this.isEof && this.nextValue == null) {
  21. if( !this.it.hasNext() ){
  22. this.isEof=true;
  23. }else{
  24. this.nextValue = new ArrayList<Integer>();
  25. for(int i=0;i<3;++i){
  26. if( this.it.hasNext() ){
  27. this.nextValue.add(this.it.next());
  28. }else{
  29. break;
  30. }
  31. }
  32. }
  33. }
  34. return !this.isEof;
  35. }
  36. @Override
  37. public List<Integer> next() {
  38. if( hasNext() ){
  39. List<Integer> result=nextValue;
  40. this.nextValue=null;
  41. return result;
  42. }
  43. }
  44. }
  45. public static void main (String[] args) throws java.lang.Exception
  46. {
  47. var stream = IntStream.range(0, 10);
  48. Spliterator<List<Integer>> spliterator = Spliterators.spliteratorUnknownSize(new Hoge(stream.iterator()), NONNULL);
  49. Stream<List<Integer>> result = StreamSupport.stream(spliterator, false);
  50. result.forEach(System.out::println);
  51. }
  52. }
  53.  
Success #stdin #stdout 0.07s 50504KB
stdin
Standard input is empty
stdout
[0, 1, 2]
[3, 4, 5]
[6, 7, 8]
[9]