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 <T> Set<T> toImmutableSet(Set<T> set) {
    switch (set.size()) {
    case 0:/*from  w w w.ja  va  2s  .c o  m*/
        return Collections.emptySet();
    case 1:
        return Collections.singleton(set.iterator().next());
    default:
        return Collections.unmodifiableSet(set);
    }
}

From source file:Main.java

public static <T> boolean overlaps(Set<T> a, Set<T> b) {
    if (a.size() > b.size()) {
        Set<T> t = a;//  w  ww .ja  va  2s.  c  o m
        a = b;
        b = t;
    }
    for (T elem : a) {
        if (b.contains(elem)) {
            return true;
        }
    }
    return false;
}

From source file:Main.java

public static <T> List<T> setToList(Set<T> set) {
    List<T> list = new ArrayList<>(set.size());
    list.addAll(set);// w  ww  .ja v  a2s  .c o  m
    return list;
}

From source file:Main.java

public static String[] clean(String[] input) {
    Set<String> unique = new TreeSet<>(Arrays.asList(input));
    return unique.toArray(new String[unique.size()]);
}

From source file:Main.java

public static <T> boolean hasDuplicates(Collection<T> collection) {
    Set<T> set = new HashSet<T>(collection);
    return set.size() != collection.size();
}

From source file:Main.java

public static boolean intersects(Set<?> a, Set<?> b) {
    if (a.size() < b.size()) {
        for (Object obj : a) {
            if (b.contains(obj)) {
                return true;
            }//from  ww  w .j  a va 2 s  . c o  m
        }
    } else {
        for (Object obj : b) {
            if (a.contains(obj)) {
                return true;
            }
        }
    }
    return false;
}

From source file:Main.java

public static <T> List<T> setToList(Set<T> set) {
    List<T> list = new ArrayList<T>(set.size());
    list.addAll(set);//from  www  .jav  a  2  s . c o  m
    return list;
}

From source file:Main.java

public static String[] toArray(Set<String> set) {
    return set.toArray(new String[set.size()]);
}

From source file:Main.java

public static String getMapString(Map map) {
    Set set = map.keySet();
    if (set.size() < 1) {
        return "[]";
    }/* ww  w  .j a v  a2s. c o m*/
    StringBuilder strBuilder = new StringBuilder();
    Object[] array = set.toArray();
    strBuilder.append("[").append(array[0]).append("=").append(map.get(array[0]));
    for (int i = 1; i < array.length; ++i) {
        strBuilder.append(", ");
        strBuilder.append(array[i]).append("=");
        strBuilder.append(map.get(array[i]));
    }
    strBuilder.append("]");
    return strBuilder.toString();
}

From source file:Main.java

private synchronized static int getLastKey() {
    Set<Integer> set = mapNotify.keySet();
    if (set.size() == 0) {
        return 0;
    }//w  ww  .  j a va2s  .  c o m
    return (int) set.toArray()[set.size() - 1];
}