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 void setPersonGroupName(String personGroupIdToAdd, String personGroupName, Context context) {
    SharedPreferences personGroupIdNameMap = context.getSharedPreferences("PersonGroupIdNameMap",
            Context.MODE_PRIVATE);

    SharedPreferences.Editor personGroupIdNameMapEditor = personGroupIdNameMap.edit();
    personGroupIdNameMapEditor.putString(personGroupIdToAdd, personGroupName);
    personGroupIdNameMapEditor.commit();

    Set<String> personGroupIds = getAllPersonGroupIds(context);
    Set<String> newPersonGroupIds = new HashSet<>();
    for (String personGroupId : personGroupIds) {
        newPersonGroupIds.add(personGroupId);
    }/*from   w ww  .jav a  2s  .  c  o m*/
    newPersonGroupIds.add(personGroupIdToAdd);
    SharedPreferences personGroupIdSet = context.getSharedPreferences("PersonGroupIdSet", Context.MODE_PRIVATE);
    SharedPreferences.Editor personGroupIdSetEditor = personGroupIdSet.edit();
    personGroupIdSetEditor.putStringSet("PersonGroupIdSet", newPersonGroupIds);
    personGroupIdSetEditor.commit();
}

From source file:Main.java

/**
 * Sorts objects in the given collection into different types by Class.
 * @param <E> the element type/*from   w w w .j  av  a 2s .  co  m*/
 * @param objects a Collection of objects
 * @param inherit if true, objects are put into all their class's superclasses as well, except
 * Object - the original Collection can be used in that case
 * @return a Map from Class to List of objects in that class
 */
public static <E> Map<Class<?>, List<E>> groupByClass(Collection<E> objects, boolean inherit) {
    Map<Class<?>, List<E>> retval = new HashMap<Class<?>, List<E>>();
    for (E o : objects) {
        Class<?> c = o.getClass();
        if (inherit) {
            Set<Class<?>> done = new HashSet<Class<?>>();
            done.add(Object.class);
            Stack<Class<?>> todo = new Stack<Class<?>>();
            todo.push(c);
            while (!todo.empty()) {
                c = todo.pop();
                if ((c != null) && !done.contains(c)) {
                    done.add(c);
                    List<E> l = retval.get(c);
                    if (l == null) {
                        l = new ArrayList<E>();
                        retval.put(c, l);
                    }
                    l.add(o);
                    todo.push(c.getSuperclass());
                    Class<?>[] classes = c.getInterfaces();
                    for (int i = 0; i < classes.length; i++) {
                        todo.push(classes[i]);
                    }
                }
            }
        } else {
            List<E> l = retval.get(c);
            if (l == null) {
                l = new ArrayList<E>();
                retval.put(c, l);
            }
            l.add(o);
        }
    }
    return retval;
}

From source file:Main.java

public static Set<String> diffSet(Set<String> set1, Set<String> set2) {
    Set<String> result = new HashSet<String>();
    for (String s : set1) {
        if (!set2.contains(s)) {
            result.add(s);
        }//from   w  w  w  . j a  v  a2  s.c  om
    }
    return result;
}

From source file:Main.java

public static <E> Collection<E> asymmetricDifference(E[] c1, Collection<E> c2) {
    final Set<E> result = new HashSet<E>();
    for (final E elem : c1) {
        if (!c2.contains(elem)) {
            result.add(elem);
        }/*  w w w .  j  ava 2 s  .  c o  m*/
    }
    return result;
}

From source file:Main.java

public static int addToWordSet(Set<String> wordSet, String words) {
    String[] r = words.split(",");
    int count = 0;

    for (String word : r) {
        wordSet.add(word);
        count++;/*w  w  w .j  av  a2s.  c o  m*/
    }

    return count;
}

From source file:Main.java

public static String removeRepeat(String s) {
    Set<Object> tempSet = new HashSet<Object>();
    String[] t = s.split(",");
    for (int i = 0; i < t.length; i++) {
        tempSet.add(t[i]);
    }/*from w ww  .  ja v a2  s  . c  o  m*/
    Object[] tempObject = tempSet.toArray();
    String temp = "";
    for (int i = 0; i < tempObject.length; i++) {
        temp += tempObject[i] + ",";
    }
    return temp;
}

From source file:Main.java

/**
 * Creates a list of the given type for the given elements.
 * // w w  w  .  j  av  a  2  s  . c  o  m
 * @param <T>
 *            The type of the elements and the list.
 * @param elements
 *            The elements to add to the list.
 * @return The filled list.
 */
public static <T> Set<T> createSet(final T... elements) {
    Set<T> coll = new HashSet<T>();
    if (elements != null) {
        for (int i = 0; i < elements.length; i++) {
            coll.add(elements[i]);
        }
    }
    return coll;
}

From source file:curly.artifactory.ArtifactoryTestHelper.java

public static Artifact createArtifact(MongoTemplate mongoTemplate) {
    mongoTemplate.findAllAndRemove(new Query(), Artifact.class);
    Artifact artifact = new Artifact();
    Set<Language> languages = new HashSet<>(0);
    Set<Tag> tags = new HashSet<>(0);
    tags.add(new Tag("document"));
    tags.add(new Tag("nosql"));
    languages.add(new Language("java"));
    languages.add(new Language("groovy"));
    languages.add(new Language("ruby"));
    languages.add(new Language("scala"));
    languages.add(new Language("javascript"));
    artifact.setName("curly");
    artifact.setDescription("a hobby project");
    artifact.setAuthor("joaoevangelista");
    artifact.setCategory(new Category("database"));
    artifact.setHomePage("http://example.com");
    artifact.setIncubation(LocalDate.now().toString());
    artifact.setLanguages(languages);//from www  . j a  v  a 2s .  c  o m
    artifact.setTags(tags);
    artifact.setOwner("6969");
    mongoTemplate.insert(artifact);
    Assert.assertTrue(artifact.getId() != null);
    try {
        System.out.println(new ObjectMapper().writeValueAsString(artifact));
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return artifact;
}

From source file:Main.java

private static void addAssignableClasses(Set<Class<?>> classes, Class<?> clazz) {
    if (notAssignableClasses.contains(clazz)) {
        return;/*from  ww w .ja v  a  2  s.  c  o  m*/
    }
    classes.add(clazz);
    final Class<?>[] interfaces = clazz.getInterfaces();
    for (int i = 0; i < interfaces.length; ++i) {
        addAssignableClasses(classes, interfaces[i]);
    }
}

From source file:Main.java

private static Set<String> getHeadSet(Set<Integer> numSet) {
    Set<String> headSet = new HashSet<String>();
    for (Integer i : numSet) {
        if (i < 4 && i > 0) {
            headSet.add("" + i);
        }//from w w  w  . j a va2s  . co m
    }
    return headSet;
}