Example usage for java.util HashSet contains

List of usage examples for java.util HashSet contains

Introduction

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

Prototype

public boolean contains(Object o) 

Source Link

Document

Returns true if this set contains the specified element.

Usage

From source file:Main.java

public static <T> Set<T> toUniqueSet(T[] array) {
    HashSet<T> set = new HashSet<T>();

    if (array != null) {
        for (T object : array) {
            if (!set.contains(object)) {
                set.add(object);//  w  w w .  j  a  va 2  s .co m
            }
        }
    }

    return set;
}

From source file:Main.java

/**
 * Returns a list containing only unique values in a collection
 */// w ww  .  j  a  v a  2 s  .c  o m
public static <T> List<T> getUnique(Collection<T> collection) {
    HashSet<T> set = new HashSet<>();
    List<T> out = new ArrayList<>();
    for (T t : collection) {
        if (!set.contains(t)) {
            out.add(t);
            set.add(t);
        }
    }
    return out;
}

From source file:Main.java

/**
 * @param c//from w w w.  ja  v a 2s . c  o m
 * @param values
 * @return <code>true</code> if the collection contains one of the values.
 */
public static <T> boolean containsAny(Collection<T> c, Collection<? extends T> values) {
    if (values == null || c == null) {
        return false;
    }
    HashSet<T> set = hashSet(c);
    for (T value : values) {
        if (set.contains(value)) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

/**
 * @param c//from  w w w  .jav  a  2s  . co  m
 * @param values
 * @return <code>true</code> if the collection contains one of the values.
 */
public static <T> boolean containsAny(Collection<T> c, T... values) {
    if (values == null || c == null) {
        return false;
    }
    HashSet<T> set = hashSet(c);
    for (T value : values) {
        if (set.contains(value)) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static void execute_crop_stmt(PreparedStatement pstmt, int[] indexes, HashSet<Integer> set)
        throws SQLException {
    int cnt = 0;/*from w w w  .  j  ava2s  .c om*/
    for (int i = 0; i < indexes.length; i++) {
        if (!set.contains(indexes[i])) {
            pstmt.setInt(1, indexes[i]);
            pstmt.addBatch();
            cnt++;
            if (cnt > 5000) {
                pstmt.executeBatch();
                cnt = 0;
            }
        }
    }
    if (cnt > 0)
        pstmt.executeBatch();
}

From source file:Main.java

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

    for (T object : set1) {
        set.add(object);/*from  w  w  w.  ja v a2s.co m*/
    }
    for (T object : set2) {
        if (!set.contains(object)) {
            set.add(object);
        }
    }

    return set;
}

From source file:Main.java

/**
 * Returns true if there any common ParcelUuids in uuidA and uuidB.
 *
 * @param uuidA - List of ParcelUuids//  w w  w  .j av  a2 s  .  c om
 * @param uuidB - List of ParcelUuids
 *
 */
public static boolean containsAnyUuid(ParcelUuid[] uuidA, ParcelUuid[] uuidB) {
    if (uuidA == null && uuidB == null)
        return true;

    if (uuidA == null) {
        return uuidB.length == 0 ? true : false;
    }

    if (uuidB == null) {
        return uuidA.length == 0 ? true : false;
    }

    HashSet<ParcelUuid> uuidSet = new HashSet<ParcelUuid>(Arrays.asList(uuidA));
    for (ParcelUuid uuid : uuidB) {
        if (uuidSet.contains(uuid))
            return true;
    }
    return false;
}

From source file:Main.java

/**
 * Returns true if all the ParcelUuids in ParcelUuidB are present in
 * ParcelUuidA//from   w w  w. j a  va2s . c o  m
 *
 * @param uuidA - Array of ParcelUuidsA
 * @param uuidB - Array of ParcelUuidsB
 *
 */
public static boolean containsAllUuids(ParcelUuid[] uuidA, ParcelUuid[] uuidB) {
    if (uuidA == null && uuidB == null)
        return true;

    if (uuidA == null) {
        return uuidB.length == 0 ? true : false;
    }

    if (uuidB == null)
        return true;

    HashSet<ParcelUuid> uuidSet = new HashSet<ParcelUuid>(Arrays.asList(uuidA));
    for (ParcelUuid uuid : uuidB) {
        if (!uuidSet.contains(uuid))
            return false;
    }
    return true;
}

From source file:fr.simon.marquis.secretcodes.util.Utils.java

public static boolean addSecretCode(Context ctx, SecretCode value) {
    HashSet<SecretCode> secretCodes = new HashSet<SecretCode>(getSecretCodes(ctx));
    boolean exist = secretCodes.contains(value);
    if (exist) {/*from w  w  w .j a  v a2s . c om*/
        secretCodes.remove(value);
    }
    secretCodes.add(value);
    saveSecretCodes(ctx, new ArrayList<SecretCode>(secretCodes));
    return !exist;
}

From source file:Main.java

public static Integer[] randomIntArray(int size) {

    Integer a[] = new Integer[size];
    HashSet<Integer> set = new HashSet<>();
    for (int x = 0; x < a.length; x++) {

        Random r = new Random();
        Integer i = r.nextInt(size * 10);

        while (set.contains(i)) {
            i = r.nextInt(size);//from   w w  w. j a v a 2  s .c o m
        }
        set.add(i);
        a[x] = i;
    }
    return a;
}