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

public static <T> Set<T> getSet() {
    return new HashSet<T>();
}

From source file:Main.java

/**
 * Thread safe method to initialize sets
 * @param initSet//from   w  ww  . j  a  va2s . com
 * @return an initialized set of the desired object type
 */
public static synchronized <T> Set<T> initializeSets(Set<T> initSet) {
    if (initSet == null) {
        return new HashSet<T>();
    } else {
        initSet.addAll(initSet);
        return initSet;
    }
}

From source file:Main.java

public static Set<String> getFavorities(Context c) {
    return c.getSharedPreferences(PREFS_NAME, 0).getStringSet(FAVORITIES, new HashSet<String>());
}

From source file:Main.java

/**
 * Many to One way of finding Keys from value
 * @param map/* www.  j a  v a 2  s.c o  m*/
 * @param value
 * @return keys
 */
public static <T, E> Set<T> getKeysByValue(Map<T, E> map, E value) {
    Set<T> keys = new HashSet<T>();
    for (Entry<T, E> entry : map.entrySet()) {
        if (value.equals(entry.getValue())) {
            keys.add(entry.getKey());
        }
    }
    return keys;
}

From source file:Main.java

public static <K, V> Map<K, Set<V>> groupByKeyEmpty(List<K> keys) {
    Map<K, Set<V>> map = new HashMap<K, Set<V>>();
    for (K key : keys) {
        if (!map.containsKey(key)) {
            map.put(key, new HashSet<V>());
        }/*from   ww  w. ja v a  2  s  . c o m*/
    }
    return map;
}

From source file:Main.java

/**
 * Returns true if every element in a collection are unique by {@link Object#equals(Object)}.
 *//* w w w  .j  a  v  a2s . c o m*/
public static boolean elementsAreUnique(Collection<?> items) {
    final Set<Object> testSet = new HashSet<>();
    for (Object item : items) {
        final boolean itemAlreadyExists = !testSet.add(item); // see Set documentation
        if (itemAlreadyExists) {
            return false;
        }
    }
    return true;
}

From source file:Main.java

private static Set<Integer> checkDuplicate(int[] intArray) {
    Set<Integer> duplicates = new HashSet<Integer>();
    Set<Integer> tmp = new HashSet<Integer>();
    for (Integer i : intArray) {
        if (!tmp.add(i)) {
            duplicates.add(i);/*from w  w w.j av a  2 s .c o m*/
        }
    }
    return duplicates;
}

From source file:Main.java

public static HashSet<String> getUserContacts(ContentResolver cr) {
    HashSet<String> contactsList = new HashSet<String>();
    //ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            // We could store names and numbers in a dictionary if we ever needed that
            // String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            if (Integer.parseInt(
                    cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null);
                while (pCur.moveToNext()) {
                    String phoneNo = pCur
                            .getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    // If the number stored isn't long enough to be in our database then don't bother.
                    if (phoneNo.length() >= 10) {
                        // Use regex to remove '+', spaces, '(', and ')' and '-' form user contacts to match database format
                        String formattedPhoneNo = phoneNo.replaceAll(DELIMITERS, "");
                        // Remove the '1' in front of the number
                        if (formattedPhoneNo.charAt(0) == '1') {
                            formattedPhoneNo = formattedPhoneNo.substring(1);
                        }// www .  j  a v a 2s .  co  m
                        contactsList.add(formattedPhoneNo);
                    }
                }
                pCur.close();
            }
        }
    }
    return contactsList;
}

From source file:Main.java

public static <T> List<T> copyWithoutDublicates(List<T> l) {
    final List<T> result = new ArrayList<T>();
    final Set<T> set = new HashSet<T>();
    for (Iterator<T> it = l.iterator(); it.hasNext();) {
        final T t = it.next();
        if (set.add(t)) {
            result.add(t);//  w  ww. j a v a 2s  .  c  o  m
        }
    }
    return result;
}

From source file:Main.java

public static void removeDuplicateWithOrder(List<ResolveInfo> list) {

    if (list == null) {

        return;/* w  w w.  ja  v a  2s  .co m*/

    }

    Set<String> set = new HashSet<String>();

    List<ResolveInfo> newList = new ArrayList<ResolveInfo>();

    for (Iterator<ResolveInfo> iter = list.iterator(); iter.hasNext();) {

        ResolveInfo info = (ResolveInfo) iter.next();

        if (set.add(info.activityInfo.packageName)) {

            newList.add(info);

        }
    }

    list.clear();

    list.addAll(newList);

}