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

/**
 * This utility method converts a vararg of Objects into a Set<T>.
 * /*from   www  . j  a  va2 s  .co  m*/
 * @param clazz the Set type to return.
 * @param objects the objects to be added to the set
 * @return a Set of objects (any equal objects will of course not be duplicated)
 * @throws ClassCastException if any of the supplied objects are not of type T.
 */
public static <T> Set<T> asSet(Class<T> clazz, Object... objects) {
    Set<T> result = new HashSet<T>();
    for (Object obj : objects) {
        @SuppressWarnings("unchecked")
        T cast = (T) obj;
        result.add(cast);
    }

    return result;
}

From source file:Main.java

/**
 * Returns a Set of String from a formatted string.
 * The format is /*from   w ww.  j  a va  2s  .  com*/
 * <pre>
 * &lt;value1&gt;,&lt;value2&gt;...,&lt;value3&gt;
 * </pre>
 *
 * @param str Formatted String.
 * @return a map of String to Set of String
 */
public static Set<String> parseStringToSet(String str) {
    Set<String> set = new HashSet<String>();
    StringTokenizer st = new StringTokenizer(str, ",");
    while (st.hasMoreTokens()) {
        set.add(st.nextToken().trim());
    }
    return set;
}

From source file:name.richardson.james.bukkit.utilities.command.AbstractCommandInvoker.java

protected static Suggester createSuggester(Iterable<Command> commands) {
    Set<String> names = new HashSet<String>();
    for (Command command : commands) {
        names.add(command.getName());
    }//from  w ww  .  j  a va  2 s. com
    return new StringSuggester(names);
}

From source file:Main.java

private static <T> Set<T> findGroupOneDelta(Collection<T> groupOne, Collection<T> groupTwo) {

    Set<T> delta = new HashSet<T>();
    for (T groupOneItem : groupOne)
        if (!groupTwo.contains(groupOneItem))
            delta.add(groupOneItem);
    return delta;
}

From source file:Main.java

private static <T> Set<T> findGroupTwoDelta(Collection<T> groupOne, Collection<T> groupTwo) {

    Set<T> delta = new HashSet<T>();
    for (T groupOneItem : groupOne)
        if (!groupTwo.contains(groupOneItem))
            delta.add(groupOneItem);
    return delta;
}

From source file:Main.java

/**
 * @param elements/*from   w w  w. ja va 2  s. co m*/
 * @param maxNumber
 * @return the maxNumber of elements in ids and rest are skipped
 */
public static <T> Set<T> getElementsOfMaxSize(Set<T> elements, int maxNumber) {
    // The returned set should maintain the same order as the input elements
    Set<T> maxIds = new LinkedHashSet<T>();

    if (maxNumber <= 0) {
        return maxIds;
    }

    for (T id : elements) {
        maxIds.add(id);

        maxNumber--;

        if (maxNumber <= 0) {
            break;
        }

    }

    elements.removeAll(maxIds);

    return maxIds;
}

From source file:at.bestsolution.efxclipse.tooling.pde.e4.project.template.E4LaunchDef.java

public static Set<PluginLaunchDef> getE4TargetPlugins() {
    Set<PluginLaunchDef> set = new HashSet<PluginLaunchDef>();
    set.add(new PluginLaunchDef("at.bestsolution.efxclipse.runtime.application"));
    set.add(new PluginLaunchDef("at.bestsolution.efxclipse.runtime.bindings.e4"));
    set.add(new PluginLaunchDef("at.bestsolution.efxclipse.runtime.bindings.generic"));
    set.add(new PluginLaunchDef("at.bestsolution.efxclipse.runtime.bindings"));
    set.add(new PluginLaunchDef("at.bestsolution.efxclipse.runtime.databinding"));
    set.add(new PluginLaunchDef("at.bestsolution.efxclipse.runtime.di"));
    set.add(new PluginLaunchDef("at.bestsolution.efxclipse.runtime.dialogs"));
    set.add(new PluginLaunchDef("at.bestsolution.efxclipse.runtime.javafx"));
    set.add(new PluginLaunchDef("at.bestsolution.efxclipse.runtime.osgi.util"));
    set.add(new PluginLaunchDef("at.bestsolution.efxclipse.runtime.osgi", "default", "false"));
    set.add(new PluginLaunchDef("at.bestsolution.efxclipse.runtime.panels"));
    set.add(new PluginLaunchDef("at.bestsolution.efxclipse.runtime.services"));
    set.add(new PluginLaunchDef("at.bestsolution.efxclipse.runtime.theme"));
    set.add(new PluginLaunchDef("at.bestsolution.efxclipse.runtime.workbench.base"));
    set.add(new PluginLaunchDef("at.bestsolution.efxclipse.runtime.workbench.fx"));
    set.add(new PluginLaunchDef("at.bestsolution.efxclipse.runtime.workbench.renderers.base"));
    set.add(new PluginLaunchDef("at.bestsolution.efxclipse.runtime.workbench.renderers.fx"));
    set.add(new PluginLaunchDef("com.ibm.icu"));
    set.add(new PluginLaunchDef("javax.annotation"));
    set.add(new PluginLaunchDef("javax.inject"));
    set.add(new PluginLaunchDef("javax.xml"));
    set.add(new PluginLaunchDef("org.apache.commons.logging"));
    set.add(new PluginLaunchDef("org.eclipse.core.commands"));
    set.add(new PluginLaunchDef("org.eclipse.core.contenttype"));
    set.add(new PluginLaunchDef("org.eclipse.core.databinding.observable"));
    set.add(new PluginLaunchDef("org.eclipse.core.databinding.property"));
    set.add(new PluginLaunchDef("org.eclipse.core.databinding"));
    set.add(new PluginLaunchDef("org.eclipse.core.expressions"));
    //      set.add(new PluginLaunchDef("org.eclipse.core.filesystem.macosx","default","false"));
    set.add(new PluginLaunchDef("org.eclipse.core.filesystem"));
    set.add(new PluginLaunchDef("org.eclipse.core.jobs"));
    set.add(new PluginLaunchDef("org.eclipse.core.resources"));
    set.add(new PluginLaunchDef("org.eclipse.core.runtime.compatibility.registry", "default", "false"));
    set.add(new PluginLaunchDef("org.eclipse.core.runtime", "default", "true"));
    set.add(new PluginLaunchDef("org.eclipse.e4.core.commands"));
    set.add(new PluginLaunchDef("org.eclipse.e4.core.contexts"));
    set.add(new PluginLaunchDef("org.eclipse.e4.core.di.extensions"));
    set.add(new PluginLaunchDef("org.eclipse.e4.core.di"));
    set.add(new PluginLaunchDef("org.eclipse.e4.core.services"));
    set.add(new PluginLaunchDef("org.eclipse.e4.ui.di"));
    set.add(new PluginLaunchDef("org.eclipse.e4.ui.model.workbench"));
    set.add(new PluginLaunchDef("org.eclipse.e4.ui.services"));
    set.add(new PluginLaunchDef("org.eclipse.e4.ui.workbench"));
    set.add(new PluginLaunchDef("org.eclipse.emf.common"));
    set.add(new PluginLaunchDef("org.eclipse.emf.databinding"));
    set.add(new PluginLaunchDef("org.eclipse.emf.ecore.change"));
    set.add(new PluginLaunchDef("org.eclipse.emf.ecore.xmi"));
    set.add(new PluginLaunchDef("org.eclipse.emf.ecore"));
    set.add(new PluginLaunchDef("org.eclipse.equinox.app"));
    set.add(new PluginLaunchDef("org.eclipse.equinox.common", "2", "true"));
    set.add(new PluginLaunchDef("org.eclipse.equinox.concurrent"));
    set.add(new PluginLaunchDef("org.eclipse.equinox.ds", "1", "true"));
    set.add(new PluginLaunchDef("org.eclipse.equinox.event"));
    set.add(new PluginLaunchDef("org.eclipse.equinox.preferences"));
    set.add(new PluginLaunchDef("org.eclipse.equinox.registry"));
    set.add(new PluginLaunchDef("org.eclipse.equinox.util"));
    set.add(new PluginLaunchDef("org.eclipse.osgi.services"));
    set.add(new PluginLaunchDef("org.eclipse.osgi", "-1", "true"));

    return set;/*from   ww  w  . j a  v a2  s. c om*/
}

From source file:Main.java

/**
 * Returns an array of values as a union set of the two input arrays.
 *
 * @param <T>//from w  ww. j  a v  a2  s  .co  m
 * @param values
 * @param newValues
 * @return the union of the two arrays
 */
@Deprecated
public static <T> T[] addWithoutDuplicates(T[] values, T[] newValues) {

    Set<T> originals = new HashSet<>(values.length);
    for (T value : values) {
        originals.add(value);
    }
    List<T> newOnes = new ArrayList<>(newValues.length);
    for (T value : newValues) {
        if (originals.contains(value)) {
            continue;
        }
        newOnes.add(value);
    }

    T[] largerOne = (T[]) Array.newInstance(values.getClass().getComponentType(),
            values.length + newOnes.size());
    System.arraycopy(values, 0, largerOne, 0, values.length);
    for (int i = values.length; i < largerOne.length; i++) {
        largerOne[i] = newOnes.get(i - values.length);
    }
    return largerOne;
}

From source file:Main.java

public static <T> List<T> removeDuplicate(List<T> list) {
    Set set = new HashSet();
    List newList = new ArrayList();
    for (Iterator iter = list.iterator(); iter.hasNext();) {
        Object element = iter.next();
        if (set.add(element))
            newList.add(element);// w w w .  j a  v  a2 s  .  c o  m
    }
    return newList;
}

From source file:MainClass.java

public static void test(Set s) {
    System.out.println(s.getClass().getName().replaceAll("\\w+\\.", ""));
    fill(s);//from  www . j av  a  2s .c om
    fill(s);
    fill(s);
    System.out.println(s); // No duplicates!

    s.addAll(s);
    s.add("one");
    s.add("one");
    s.add("one");
    System.out.println(s);

    System.out.println("s.contains(\"one\"): " + s.contains("one"));
}