public static <T> Set<T> collectionToSet(Collection<T> collection) {
        if (collection instanceof Set) {
            return (Set<T>) collection;
        } else {
            switch (collection.size()) {
                case 0  : return emptySet();
                case 1  : return singleton(collection.iterator().next());
                default : return new HashSet<>(collection);
            }
        }
    }
Returns a {@link Set} with the values in the specified collection. If the
specified collection is already a {@link Set}, it might be returned. This
method makes no guarantees regarding mutability or iteration order of the
returned set.

@param <T>         the element type
@param collection  the collection
@return            a set with the same elements as the collection
runtime-parent/runtime-field/src/main/java/com/speedment/runtime/field/internal/util/CollectionUtil.java