Example usage for java.util HashSet HashSet

List of usage examples for java.util HashSet HashSet

Introduction

In this page you can find the example usage for java.util HashSet HashSet.

Prototype

public HashSet(int initialCapacity) 

Source Link

Document

Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and default load factor (0.75).

Usage

From source file:Main.java

public static <T> Set<Set<T>> extractSubSets(Set<T> initialSet, int subSetSize) {
    int nbSources = initialSet.size();
    int expectedNumberOfSets = expectedNumberOfSets(nbSources, subSetSize);
    Set<Set<T>> setOfSets = new HashSet<>(expectedNumberOfSets);
    if (nbSources == subSetSize) {
        // Already OK
        setOfSets.add(initialSet);/* www . j a v a2  s. c  o  m*/
        return setOfSets;
    }
    List<T> setAsList = new ArrayList<>(initialSet);
    int[] iterators = new int[subSetSize];
    for (int i = 0; i < iterators.length; i++) {
        iterators[i] = i;
    }
    while (setOfSets.size() != expectedNumberOfSets) {
        HashSet<T> result = new HashSet<>(subSetSize);
        for (int pos : iterators) {
            result.add(setAsList.get(pos));
        }
        if (result.size() != subSetSize) {
            throw new IllegalStateException("Hard!");
        }
        setOfSets.add(result);
        int maxPos = -1;
        for (int i = 0; i < iterators.length; i++) {
            int pos = iterators[i];
            if (pos == (nbSources - iterators.length + i)) {
                maxPos = i;
                break;
            }
        }
        if (maxPos == -1) {
            // Up last iterator
            iterators[iterators.length - 1]++;
        } else if (maxPos == 0) {
            // Finished
            if (setOfSets.size() != expectedNumberOfSets) {
                System.err.println("Something wrong!");
            }
        } else {
            // Up the one before maxPos and reinit the others
            iterators[maxPos - 1]++;
            for (int i = maxPos; i < iterators.length; i++) {
                iterators[i] = iterators[i - 1] + 1;
            }
        }
    }
    return setOfSets;
}

From source file:Main.java

/** Shortcut for creating a Set collection. Used for the list of required and unbound parameters. */
public static <T2> Set<T2> unmodifiableSet(T2... items) {
    return Collections.unmodifiableSet(new HashSet<T2>(Arrays.asList(items)));
}

From source file:Main.java

public static <T> Set<T> copy(Set<? extends T> a) {
    return new HashSet<T>(a);
}

From source file:Main.java

public static Set<String> getLockedApps(Context c) {
    SharedPreferences sp = appsPrefs(c);
    return new HashSet<>(sp.getAll().keySet());
}

From source file:Main.java

/**
 * Returns the items as a populated set.
 *
 * @param items//from w  w  w. j av  a2  s. c om
 *            Object[]
 * @return Set
 */
public static <T> Set<T> asSet(T[] items) {

    return new HashSet<>(Arrays.asList(items));
}

From source file:Main.java

private static Map<String, Set<String>> convertHashMapToTypeSet(Map<String, String> hashMap) {
    Map<String, Set<String>> globalVars = new HashMap<String, Set<String>>();

    for (final Map.Entry<String, String> entry : hashMap.entrySet()) {
        globalVars.put(entry.getKey(), new HashSet<String>(Arrays.asList(entry.getValue())));
    }/*  w  w w.  j  a  v  a  2s .  com*/

    return globalVars;
}

From source file:Main.java

public static <T> Set<T> immutableSet(Collection<? extends T> set) {
    return Collections.unmodifiableSet(set instanceof Set ? (Set<? extends T>) set : new HashSet<T>(set));
}

From source file:Main.java

/**
 * Function to remove duplicate elements from the array of string
 * @param stringArray array of string having duplicate elements.
 * @return unique array of string /*from  w  w w . j  a v a2 s.  c o m*/
 */
public static String[] removeDuplicatesFrom(String[] stringArray) {
    Set<String> temp = new HashSet<String>(Arrays.asList(stringArray));
    String[] uniqueStringArray = temp.toArray(new String[temp.size()]);
    return uniqueStringArray;
}

From source file:Main.java

public static Set toSet(List someElements) {
    Set asSet = new HashSet(someElements.size());
    for (int i = 0; i < someElements.size(); i++)
        asSet.add(someElements.get(i));/*from w  ww . java  2  s .co m*/

    return asSet;
}

From source file:Main.java

@SafeVarargs
public static <T> Set<T> asSet(T... ts) {
    return new HashSet<>(Arrays.asList(ts));
}