Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.*;

public class Main {
    /**
     * Split an iterator into chunks -- this is useful for, e.g., splitting work among threads when it
     * input iterator has very short jobs.
     *
     * @param input The input iterator to chunk.
     * @param chunkSize The number of elements to include in each chunk (except the last one).
     * @param <E> The element type of the input iterator.
     * @return An iterator corresponding to the input, but chunked into groups.
     */
    public static <E> Iterator<Collection<E>> chunk(final Iterator<E> input, final int chunkSize) {
        return new Iterator<Collection<E>>() {
            @Override
            public synchronized boolean hasNext() {
                return input.hasNext();
            }

            @Override
            public synchronized Collection<E> next() {
                List<E> rtn = new ArrayList<>();
                for (int i = 0; i < chunkSize; ++i) {
                    if (input.hasNext()) {
                        rtn.add(input.next());
                    }
                }
                return rtn;
            }

            @Override
            public void remove() {
                throw new RuntimeException("Remove is not implemented");
            }
        };
    }
}