Iterables { public static <T> Iterable<T> concat(Iterable<? extends T> it1, Iterable<? extends T> it2) { return () -> new Iterator<T>() { boolean first = true; Iterator<? extends T> it = it1.iterator(); @Override public boolean hasNext() { boolean hasNext = it.hasNext(); if(hasNext) return true; if(first) { first = false; it = it2.iterator(); return hasNext(); } return false; } @Override public T next() { if(first) { hasNext(); } return it.next(); } }; }  static Iterable<T> concat(Iterable<? extends T> it1, Iterable<? extends T> it2); static List<T> toList(Iterable<T> iterable);  }
@Test public void testConcat() { assertEquals(Arrays.asList(1, 2, 3), Iterables .toList(Iterables.concat(Arrays.asList(1, 2, 3), Arrays.asList()))); assertEquals(Arrays.asList(1, 2, 3), Iterables .toList(Iterables.concat(Arrays.asList(), Arrays.asList(1, 2, 3)))); assertEquals(Arrays.asList(), Iterables.toList(Iterables.concat(Arrays.asList(), Arrays.asList()))); assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6), Iterables.toList(Iterables.concat(Arrays.asList(1, 2, 3), Arrays .asList(4, 5, 6)))); }