Example usage for java.util Set add

List of usage examples for java.util Set add

Introduction

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

Prototype

boolean add(E e);

Source Link

Document

Adds the specified element to this set if it is not already present (optional operation).

Usage

From source file:Main.java

public static <E extends Enum<?>> Set<E> getAllExcluding(E elements[], E... excluding) {
    Set<E> exclude_set = new HashSet<E>();
    for (E e : excluding)
        exclude_set.add(e);

    Set<E> elements_set = new HashSet<E>();
    for (int i = 0; i < elements.length; i++) {
        if (!exclude_set.contains(elements[i]))
            elements_set.add(elements[i]);
    } // FOR//  w w  w. j  a  v a 2  s  .  co  m
    return (elements_set);
    //      Crappy java....
    //        Object new_elements[] = new Object[elements_set.size()];
    //        elements_set.toArray(new_elements);
    //        return ((E[])new_elements);
}

From source file:Main.java

public static <T> Set<T> toSet(Iterable<? extends T> things) {
    if (things == null) {
        return new HashSet<T>(0);
    }/*from  w w  w. j av  a 2  s  . c  om*/
    if (things instanceof Set) {
        @SuppressWarnings("unchecked")
        Set<T> castThings = (Set<T>) things;
        return castThings;
    }

    Set<T> set = new LinkedHashSet<T>();
    for (T thing : things) {
        set.add(thing);
    }
    return set;
}

From source file:Main.java

/**
 * Converts parameters into a set/*from ww  w .  j a  v a2s  .  com*/
 *
 * @param <T>    set element type
 * @param first  element
 * @param second element
 * @param third  element
 * @return set containing the given elements
 */
public static <T> Set<T> asSet(T first, T second, T third) {
    Set<T> set = new HashSet<>(3);
    set.add(first);
    set.add(second);
    set.add(third);
    return set;
}

From source file:Main.java

/**
 * <p>Return a "type-safe" set/*  w  ww .j a v a 2  s.  c om*/
 * </p>
 *
 * @param iterator the iterator to build the set out of
 * @param <T> elements type
 * @return a type-safe {@link HashSet}
 */
public static <T> Set<T> toSet(final Iterator<T> iterator) {
    final Set<T> ret = new HashSet<T>();

    while (iterator.hasNext())
        ret.add(iterator.next());

    return ret;
}

From source file:edu.umd.umiacs.clip.tools.scor.WordVectorUtils.java

public static void subset(String input, String output, Set<String> words, boolean loadOldVectorToMemory) {
    Set<String> allWords = new HashSet<>(words);
    allWords.add("</s>");
    List<String> lines = new ArrayList<>();
    (loadOldVectorToMemory ? readAllLines(input).parallelStream() : lines(input)).filter(line -> {
        String[] fields = line.split(" ");
        return fields.length > 2 && allWords.contains(fields[0]);
    }).forEach(lines::add);//from   www  . j a  va2s .c o  m
    lines.add(0, lines.size() + " " + (lines.get(0).split(" ").length - 1));
    write(output, lines, REMOVE_OLD_FILE);
}

From source file:Main.java

private static Integer _findDuplicate(int[] array) {
    Set<Integer> seenElements = new LinkedHashSet<>();

    for (int element : array) {
        if (!seenElements.add(element)) {
            return element;
        }/*  w w w . j  a va 2  s. c o m*/
    }

    return null;
}

From source file:Main.java

/**
 * Build a set of bean from the provided iterable
 * @param source any kind of iterable// ww w  .j  a  va 2 s .  com
 * @return a {@link Serializable} {@link Set} containing the same element set
 */
public static <Bean> Set<Bean> asSet(Iterable<Bean> source) {
    if (source instanceof Set && source instanceof Serializable) {
        return (Set<Bean>) source;
    } else if (source == null) {
        return Collections.emptySet();
    } else {
        Set<Bean> returned = new HashSet<Bean>();
        for (Bean b : source) {
            returned.add(b);
        }
        return returned;
    }
}

From source file:Main.java

public static <T> Set<T> intersection(Set<T> setA, Set<T> setB) {
    Set<T> tmp = new TreeSet<T>();
    for (T x : setA)
        if (setB.contains(x))
            tmp.add(x);
    return tmp;/* w ww  . j  a va  2s .com*/
}

From source file:Main.java

public static <T> Set<T> toSet(final Iterable<T> iterable) {
    final Set<T> set = new TreeSet<T>();
    for (Iterator<T> i = iterable.iterator(); i.hasNext();) {
        set.add(i.next());
    }/*from ww  w.  ja  v  a 2  s.com*/
    return set;
}

From source file:Main.java

/**
 * <p>Turn source list to Set</p>
 * @param list source list//  w w  w .ja  v a 2  s  . c  o m
 * @return
 */
public static <T extends Object> Set<T> turnListToSet(List<T> list) {
    Set set = new HashSet<T>();
    if (list != null) {
        for (T obj : list) {
            set.add(obj);
        }
    }
    return set;
}