Example usage for java.util HashSet add

List of usage examples for java.util HashSet add

Introduction

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

Prototype

public boolean add(E e) 

Source Link

Document

Adds the specified element to this set if it is not already present.

Usage

From source file:Main.java

public static <T, V extends T> HashSet<T> createHashSet(V... args) {
    if ((args == null) || (args.length == 0)) {
        return new HashSet();
    }//  www .j a va 2 s.  co  m
    HashSet<T> set = new HashSet(args.length);
    for (V v : args) {
        set.add(v);
    }
    return set;
}

From source file:Main.java

public static <T> HashSet<T> newHashSet(Iterable<? extends T> iterable) {
    HashSet<T> set = newHashSet();
    for (T t : iterable) {
        set.add(t);
    }/*from  ww  w .j  av a  2  s . c o m*/
    return set;
}

From source file:Main.java

/**
 * Creates a new set with elements being results of mapping
 * the function over the original set./*from w  ww.java 2s.  c o m*/
 * @param xs original set
 * @param f function
 * @param <T> original set element type, also function parameter type
 * @param <U> function result type
 * @return set of results of mapping the function
 */
@Nonnull
public static <T, U> Set<U> setMap(@Nonnull Set<T> xs, @Nonnull Function<T, U> f) {
    HashSet<U> result = new HashSet<U>(xs.size());
    for (T x : xs) {
        result.add(f.apply(x));
    }
    return result;
}

From source file:Main.java

/**
 * install focus forward and backward//from   w  w  w.  j  a  v  a  2  s  .c o  m
 */
public static void installDefaultFocusHandling(Container c) {
    // focus TAB
    HashSet<KeyStroke> set = new HashSet<KeyStroke>(1);
    set.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, 0));
    c.setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, set);

    // focus shift-TAB
    set = new HashSet<KeyStroke>(1);
    set.add(KeyStroke.getKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_MASK));
    c.setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, set);

    // make input map WHEN_FOCUSED non-empty for Focus Traversal policy to work
    // correctly
    if (c instanceof JComponent) {
        JComponent jc = (JComponent) c;
        InputMap inputMapWhenFocused = jc.getInputMap(JComponent.WHEN_FOCUSED);
        if (inputMapWhenFocused.size() == 0) {
            inputMapWhenFocused.put(KeyStroke.getKeyStroke(KeyEvent.VK_STOP, KeyEvent.KEY_TYPED),
                    "swingDummyFocusKey");
        }
    }
}

From source file:Main.java

public static Node getNodeWithKey(Node parent, String key, String value) {
    HashSet<String> values = new HashSet<>();
    values.add(value);
    List<Node> nodes = getNodesWithKey(parent, key, values, false);
    if (nodes.size() < 1)
        return null;
    return nodes.get(0);
}

From source file:Sets.java

/**
 * Construct a new {@link HashSet} with the contents of the provided {@link Iterable}, taking advantage of type inference to
 * avoid specifying the type on the rhs.
 *///from   w w w . ja  v a2s .c o  m
public static <E> HashSet<E> newHashSet(Iterable<? extends E> elements) {
    HashSet<E> set = newHashSet();
    for (E e : elements) {
        set.add(e);
    }
    return set;
}

From source file:org.podcastpedia.web.caching.EnhancedDefaultKeyGenerator.java

private static HashSet<Class<?>> getWrapperTypes() {
    HashSet<Class<?>> ret = new HashSet<Class<?>>();
    ret.add(Boolean.class);
    ret.add(Character.class);
    ret.add(Byte.class);
    ret.add(Short.class);
    ret.add(Integer.class);
    ret.add(Long.class);
    ret.add(Float.class);
    ret.add(Double.class);
    ret.add(Void.class);
    return ret;/*from w w  w.  j  a va2  s  .  co  m*/
}

From source file:Main.java

public static HashSet<Integer> toHashSet(int[] values) {
    if (values == null) {
        return null;
    }/*from  w w  w  . j av  a2  s  . com*/
    final HashSet<Integer> res = new HashSet<Integer>();
    for (int state : values) {
        res.add(state);
    }
    return res;
}

From source file:Main.java

public static <T> HashSet<T> hashSet(T value) {
    HashSet<T> set = new HashSet<T>();
    if (value != null) {
        set.add(value);
    }//from  www  .  j a  va 2s .co m
    return set;
}

From source file:Main.java

public static <T> Set<T> intersect(Collection<T> set1, Collection<T> set2) {
    HashSet<T> set = new HashSet<T>();

    for (T object : set1) {
        if (set2.contains(object)) {
            set.add(object);
        }/*from  w  w  w .j ava 2s  .c  o m*/
    }

    return set;
}