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() 

Source Link

Document

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

Usage

From source file:Main.java

/**
 * Returns a new empty HashSet for objects of a certain type
 *///  w w w  .j  av  a2s  .co m
public static <T> HashSet<T> newHashSet() {
    return new HashSet<T>();
}

From source file:Main.java

static int countDistinctElements(List source, int position, int count) {
    List list = source.subList(position, position + count);
    Set set = new HashSet();
    set.addAll(list);//from   w w  w  .ja  v  a  2s  .co  m
    return set.size();
}

From source file:Main.java

public static <T> ArrayList<T> union(Collection<T> l1, Collection<T> l2) {
    Set<T> s = new HashSet<>();
    s.addAll(l1);/*from   w  w  w. j a v  a 2 s. c o  m*/
    s.addAll(l2);
    return new ArrayList<>(s);
}

From source file:Main.java

/**
 * @param toReturn/*from www .  j  av a  2s  . co m*/
 *          the {@link Set} to return, if it is not <code>null</code>.
 * @return the given {@link Set}, or an empty {@link Set} if the given one is
 *         empty.
 */
public static <T> Set<T> emptyIfNull(Set<T> toReturn) {
    if (toReturn == null) {
        return new HashSet<T>();
    }
    return toReturn;
}

From source file:Main.java

/**
 * Creates a list of the given type for the given elements.
 * /*w w w .ja  va  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:Main.java

/**
 * Returns the set of all elements in the given collection that appear more than once.
 * @param input some collection./*  w  w  w  . java  2s .  co  m*/
 * @return the set of repeated elements.  May return an empty set, but never null.
 */
public static <T> Set<T> duplicatedElementsOf(Iterable<T> input) {
    Set<T> duplicates = new HashSet<>();
    Set<T> elementSet = new HashSet<>();
    for (T el : input) {
        if (!elementSet.add(el)) {
            duplicates.add(el);
        }
    }
    return duplicates;
}

From source file:Main.java

/**
 * Input is "daddy[8880],sindhu[8880],camille[5555]. Return List of
 * InetSocketAddress//from   www .j  a v  a 2s.  c o  m
 */
public static List<InetSocketAddress> parseCommaDelimitedHosts2(String hosts, int port_range) {

    StringTokenizer tok = new StringTokenizer(hosts, ",");
    String t;
    InetSocketAddress addr;
    Set<InetSocketAddress> retval = new HashSet<InetSocketAddress>();

    while (tok.hasMoreTokens()) {
        t = tok.nextToken().trim();
        String host = t.substring(0, t.indexOf('['));
        host = host.trim();
        int port = Integer.parseInt(t.substring(t.indexOf('[') + 1, t.indexOf(']')));
        for (int i = port; i < port + port_range; i++) {
            addr = new InetSocketAddress(host, i);
            retval.add(addr);
        }
    }
    return new LinkedList<InetSocketAddress>(retval);
}

From source file:Main.java

public static List<String> getLoggedInUsersAsList(Context context) {
    Set<String> users = PreferenceManager.getDefaultSharedPreferences(context)
            .getStringSet(LOGGED_IN_USER_PREFERENCE_KEY, new HashSet<>());
    return new ArrayList<String>(users);
}

From source file:Main.java

public static HashSet<String> getExcludedTags(Context context) {
    String tags = PreferenceManager.getDefaultSharedPreferences(context).getString("excludeTagsInheritance",
            null);//from   w ww . j  ava  2s  .c  o m

    if (tags == null)
        return null;

    HashSet<String> tagsSet = new HashSet<String>();
    for (String tag : tags.split(":")) {
        if (TextUtils.isEmpty(tag) == false)
            tagsSet.add(tag);
    }

    return tagsSet;
}

From source file:Main.java

/**
 * Answer a set containing all elements that are both in source and in query
 * Delete these elements from source Creation date: (13.10.2002 16:23:00)
 * // w w w. ja  v a 2 s.  c o m
 * @return java.util.Set
 * @param source
 *            java.util.Set
 * @param elements
 *            java.util.Collection
 */
public static Set extractFrom(Set source, Collection query) {
    Set answer = new HashSet();
    Iterator i = query.iterator();
    while (i.hasNext()) {
        Object o = i.next();
        if (source.remove(o)) {
            answer.add(o);
        }
    }
    return answer;
}