Iterables { public static <T> Set<T> toSet(Iterable<T> iterable) { if (iterable instanceof Collection) { return new LinkedHashSet<T>((Collection<T>)iterable); } else { Set<T> list = new LinkedHashSet<T>(); for (T element : iterable) { list.add(element); } return list; } } Iterables(); static List<T> toList(Iterable<T> iterable); static Set<T> toSet(Iterable<T> iterable); static String toString(Iterable<?> iterable); static int count(Iterable<?> iterable);  }
@Test public void testToSet() { Assert.assertTrue("should be an instance of LinkedHashSet", Iterables.toSet(DUMMY_COLLECTION) instanceof LinkedHashSet); Assert.assertTrue("should have 4 elements", Iterables.count(Iterables.toSet(DUMMY_COLLECTION)) == 4); Assert.assertEquals("[1, 2, 3, 4]", Iterables.toString(Iterables.toSet(DUMMY_COLLECTION))); Assert.assertTrue("should be an instance of LinkedHashSet", Iterables.toSet(DUMMY_ITERABLE) instanceof LinkedHashSet); Assert.assertTrue("should have 4 elements", Iterables.count(Iterables.toSet(DUMMY_ITERABLE)) == 4); Assert.assertEquals("[1, 2, 3, 4]", Iterables.toString(Iterables.toSet(DUMMY_ITERABLE))); }