Here you can find the source of create(Collection
Parameter | Description |
---|---|
elements | a parameter |
public static <T> Set<T> create(Collection<T> coll)
//package com.java2s; // Clark & Parsia, LLC parts of this source code are available under the terms of the Affero General Public License v3. import java.util.Collection; import java.util.Collections; import java.util.HashSet; import java.util.Set; public class Main { /**/*from www.j av a 2 s . co m*/ * * Creates a list containing all the elements in the array * * @param elements * @return */ public static <T> Set<T> create(T... elems) { Set<T> set = new HashSet<T>(elems.length); for (int i = 0; i < elems.length; i++) set.add(elems[i]); return set; } /** * * Creates a set containing all the elements in the collection * * @param elements * @return */ public static <T> Set<T> create(Collection<T> coll) { return new HashSet<T>(coll); } /** * Adds the given object to the set but saves memory space * by allocating only the required amount for small sets. The * idea is to use the specialized empty set and singleton set * implementations (which are immutable) for the sets of size * 0 and 1. If the set is empty a new singleton set is created, * if set has one element we create a new set with two elements, * otherwise we simply add the element to the given set.This * technique is most useful if the expected set size is 0 or 1. * * @param o * @param set * @return */ public static <T> Set<T> add(T o, Set<T> set) { int size = set.size(); if (size == 0) set = singleton(o); else if (size == 1) { T existing = set.iterator().next(); if (!existing.equals(o)) set = binary(existing, o); } else set.add(o); return set; } public final static <T> Set<T> singleton(T o) { return Collections.singleton(o); } /** * Checks if one set is equal of another one * * @param sub * @param sup * @return */ public static <T> boolean equals(Set<T> s1, Set<T> s2) { return s1.size() == s2.size() && s1.containsAll(s2); } public final static <T> Set<T> binary(T o1, T o2) { Set<T> set = new HashSet<T>(); set.add(o1); set.add(o2); return set; } }