Example usage for java.util Set size

List of usage examples for java.util Set size

Introduction

In this page you can find the example usage for java.util Set size.

Prototype

int size();

Source Link

Document

Returns the number of elements in this set (its cardinality).

Usage

From source file:Main.java

public static boolean containsDuplicates(Collection<?> coll) {
    if (coll == null)
        throw new IllegalArgumentException();

    Set<Object> set = new HashSet<>(coll);

    return set.size() != coll.size();
}

From source file:Main.java

public static byte[][] toArray(Set<byte[]> set) {
    if (set == null || set.size() == 0)
        return new byte[0][];

    return set.toArray(new byte[set.size()][]);
}

From source file:Main.java

public static Set<Integer> randomSetRange(int min, int max, int n) {
    Set<Integer> set = new HashSet<Integer>();
    while (set.size() < n) {
        int num = (int) (Math.random() * (max - min)) + min;
        set.add(num);/*from w  w  w .  j ava2 s  . c o m*/
    }
    return set;

}

From source file:Main.java

/**
 * Function to check if duplication elements are present in array or not.
 * @param contacts array of string//from  w  ww  .ja  v a 2 s.c o m
 * @return true if duplication is present else return false.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static boolean isDuplicatePresent(String[] contacts) {
    List inputList = Arrays.asList(contacts);
    Set inputSet = new HashSet(inputList);
    if (inputSet.size() < inputList.size())
        return true;
    return false;
}

From source file:Main.java

public static void assertAllDifferent(List<?> entities) {

    Set<?> set = new LinkedHashSet<>(entities);

    if (set.size() != entities.size()) {
        throw new IllegalArgumentException("All entities must be different: " + entities + ", " + set);
    }//from   w  w  w.  j av  a2 s  .  com

}

From source file:Main.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static boolean containsOnly(Collection collection, Object object) {
    if (collection == null)
        return false;
    Set duplicatesRemoved = new HashSet(collection);
    return duplicatesRemoved.size() == 1 && duplicatesRemoved.contains(object);
}

From source file:Main.java

public static Set intersection(Set one, Set two) {
    Set big, small;//  w w w . ja  v  a  2s .co m
    if (one.size() >= two.size()) {
        big = new HashSet(one);
        small = two;
    } else {
        big = new HashSet(two);
        small = one;
    }
    big.removeAll(small);
    return big;
}

From source file:Main.java

public static <T> Set<T> removeSorted(Set<T> set, T element) {
    final int size = set.size();
    if (size == 0 || (size == 1 && set.contains(element))) {
        return ImmutableSet.of();
    } else {// w  w  w.ja  va  2 s .  c  om
        set.remove(element);
    }
    return set;
}

From source file:Main.java

/**
 * Returns the same set <code>theSet</code> if it has elements,
 * otherwise it returns a new <code>HashSet</code>.
 * Can be used in combination with {@link CollectionUtil#emptySetIfNull(Set)} to
 * use a singleton empty set as initial value.
 * <p>/*from  ww w  .j av a  2s .c  o  m*/
 * <strong>Pre:</strong> <code>theSet != null</code>
 * </p>
 * <p>
 * <strong>Post:</strong> <code>result</code> is writable. 
 * @param <T> Element type of the Set
 * @param theSet A <code>Set</code>
 * @return The same set if <code>theSet.size() > 0</code> or a new <code>HashSet</code>  
 */
public static <T> Set<T> initAsHashSet(final Set<T> theSet) {
    if (theSet.size() == 0)
        return new HashSet<T>();
    else
        return theSet;
}

From source file:Main.java

public final static <T> Set<T> unicon(Set<T> set1, Set<T> set2) {
    Set<T> set = new HashSet<>(set1.size() + set2.size());
    set = set1;// w  w  w . ja  v a  2s  . c om
    set.addAll(set2);
    return set;
}