Java tutorial
//package com.java2s; //License from project: LGPL import java.lang.reflect.Array; import java.util.Iterator; import java.util.NoSuchElementException; public class Main { /** * Create new {@link Iterable} object which combine the first and the second * parameters. * * @param <T> * Type of the items in the array of the first specified * parameter, this type also specify the type of the arrays of * the which will be returned by the returned {@link Iterable} * @param <E> * Type of the second {@link Iterable} arrays. It may be the same * type as the first {@link Iterable}, or array of any other * subclass * @param firstIterable * {@link Iterable}, the first {@link Iterable} which contains * items * @param secondIterable * {@link Iterable}, the second {@link Iterable} which contains * items * @return {@link Iterable}, object which will return {@link Iterator}s * which will iterate over the second {@link Iterable} parameter as * many times as there are items in the first {@link Iterable} * parameter. The returned arrays will contains every possible * combination between items in the first {@link Iterable}, and the * second {@link Iterable}. For example: if the first * {@link Iterable} contains <code>{1, 2}</code> and the second * {@link Iterable} contains <code>{'a', 'b', 'c'}</code> the * returned {@link Iterable} object will dynamically combine the * {@link Iterable}s and provide following combination in specified * order: * <code>{1, 'a'}, {1, 'b'}, {1, 'c'}, {2, 'a'}, {2, 'b'}, {2, 'c'}</code> */ public static <T, E extends T> Iterable<T[]> combineIterables(final Iterable<T[]> firstIterable, final Iterable<E[]> secondIterable) { return new Iterable<T[]>() { /** * {@inheritDoc} */ public Iterator<T[]> iterator() { return new Iterator<T[]>() { private Iterator<T[]> firstArrayIterator = firstIterable.iterator(); private final Iterator<E[]> secondArrayIterator = secondIterable.iterator(); private T[] appendArray = secondArrayIterator.next(); /** * {@inheritDoc} */ public boolean hasNext() { return firstArrayIterator.hasNext() || secondArrayIterator.hasNext(); } /** * {@inheritDoc} */ public T[] next() { if (!hasNext()) { throw new NoSuchElementException(); } if (!firstArrayIterator.hasNext()) { firstArrayIterator = firstIterable.iterator(); appendArray = secondArrayIterator.next(); } T[] streamsItem = firstArrayIterator.next(); @SuppressWarnings("unchecked") T[] result = (T[]) Array.newInstance(streamsItem.getClass().getComponentType(), streamsItem.length + appendArray.length); System.arraycopy(streamsItem, 0, result, 0, streamsItem.length); System.arraycopy(appendArray, 0, result, streamsItem.length, appendArray.length); return result; } /** * {@inheritDoc} */ public void remove() { throw new UnsupportedOperationException(); } }; } }; } }