Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.util.*;

public class Main {
    /**
     * Repeats the same iterable over and over.
     * @param iterable to repeat
     * @param n specifies how often the iterable is repeated
     * @return n times repeated iterbale
     */
    public static <T> Iterable<T> cycle(Iterable<T> iterable, int n) {
        return () -> new Iterator<T>() {
            private Iterator<T> iterator = iterable.iterator();
            private int count = 0;

            @Override
            public boolean hasNext() {
                if (!iterator.hasNext()) {
                    iterator = iterable.iterator();
                    count++;
                }
                return n < 0 || count < n;
            }

            @Override
            public T next() {
                return iterator.next();
            }
        };
    }
}