Java tutorial
//package com.java2s; /* * Hibernate Validator, declare and validate application constraints * * License: Apache License, Version 2.0 * See the license.txt file in the root directory or <http://www.apache.org/licenses/LICENSE-2.0>. */ import java.util.Collection; import java.util.HashSet; public class Main { public static <T> HashSet<T> newHashSet() { return new HashSet<T>(); } public static <T> HashSet<T> newHashSet(int size) { return new HashSet<T>(getInitialCapacityFromExpectedSize(size)); } public static <T> HashSet<T> newHashSet(Collection<? extends T> c) { return new HashSet<T>(c); } public static <T> HashSet<T> newHashSet(Iterable<? extends T> iterable) { HashSet<T> set = newHashSet(); for (T t : iterable) { set.add(t); } return set; } /** * As the default loadFactor is of 0.75, we need to calculate the initial capacity from the expected size to avoid * resizing the collection when we populate the collection with all the initial elements. We use a calculation * similar to what is done in {@link HashMap#putAll(Map)}. * * @param expectedSize the expected size of the collection * @return the initial capacity of the collection */ private static int getInitialCapacityFromExpectedSize(int expectedSize) { if (expectedSize < 3) { return expectedSize + 1; } return (int) ((float) expectedSize / 0.75f + 1.0f); } }