fork download
  1. import java.util.*;
  2.  
  3. class Example {
  4. public static void main(String[] args) {
  5. List<Integer> list = Arrays.asList(1, 2, 3);
  6.  
  7. multiply(list, 3).stream()
  8. .forEach(System.out::println);
  9. }
  10.  
  11. static <E> Collection<E> multiply(Collection<E> source, int count) {
  12. return new AbstractCollection<E>() {
  13. @Override
  14. public int size() {
  15. return count * source.size();
  16. }
  17. @Override
  18. public Iterator<E> iterator() {
  19. return new Iterator<E>() {
  20. final Iterator<E> it = source.iterator();
  21.  
  22. E next;
  23. int i = 0;
  24.  
  25. @Override
  26. public boolean hasNext() {
  27. return i < size();
  28. }
  29. @Override
  30. public E next() {
  31. if (hasNext()) {
  32. if ((i % count) == 0) {
  33. next = it.next();
  34. }
  35. ++i;
  36. return next;
  37. } else {
  38. }
  39. }
  40. };
  41. }
  42. };
  43. }
  44. }
Success #stdin #stdout 0.18s 320576KB
stdin
Standard input is empty
stdout
1
1
1
2
2
2
3
3
3