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

@SuppressWarnings("unchecked")
public static <E extends Object> E[] toArray(Set<E> set, Class<E> clazz) {
    return set.toArray((E[]) Array.newInstance(clazz, set.size()));
}

From source file:com.net2plan.gui.utils.NoRunnableCodeFound.java

private static String[] toString(Set<Class<? extends IExternal>> _classes) {
    String[] out = new String[_classes.size()];
    int i = 0;/* w  ww .  j a v a 2  s .c om*/
    for (Class<? extends IExternal> _class : _classes)
        out[i++] = _class.getName();

    return out;
}

From source file:Main.java

public static <T> Set<T> addSorted(Set<T> set, T element) {
    final int size = set.size();
    if (size == 0) {
        return ImmutableSet.of(element);
    } else if (set instanceof ImmutableSet) {
        if (size == 1) {
            final T val = set.iterator().next();
            set = Sets.newLinkedHashSet();
            set.add(val);
        } else {/*from w ww.  j  a va 2 s.  c o  m*/
            set = Sets.newLinkedHashSet(set);
        }
    }
    set.add(element);
    return set;
}

From source file:Main.java

public static <T> Set<T> add(Set<T> set, T element) {
    final int size = set.size();
    if (size == 0) {
        return ImmutableSet.of(element);
    } else if (set instanceof ImmutableSet) {
        if (size == 1) {
            final T val = set.iterator().next();
            set = Sets.newHashSet();/*ww w . ja v a  2s.co m*/
            set.add(val);
        } else {
            set = Sets.newHashSet(set);
        }
    }
    set.add(element);
    return set;
}

From source file:Main.java

public static String generateTql(List<String> fields, String api, Map<String, String> params) {
    params.put("is_mobile", "true");
    String tql = "select ";
    for (int i = 0; i < fields.size(); i++) {
        if (i == fields.size() - 1) {
            tql += fields.get(i);//from  ww w. ja va 2  s . co m
        } else {
            tql += fields.get(i);
            tql += ",";
        }
    }
    tql += " from " + api;
    tql += " where ";
    Set<Entry<String, String>> entrySet = params.entrySet();
    int size = entrySet.size();
    for (Entry<String, String> entry : entrySet) {
        tql += entry.getKey() + " = " + entry.getValue();
        size--;
        if (size <= 0) {

        } else {
            tql += " and ";
        }
    }
    return tql;
}

From source file:Main.java

/**
 * Creates a new set with elements being results of mapping
 * the function over the original set./*from w  w w. jav  a2s . c o m*/
 * @param xs original set
 * @param f function
 * @param <T> original set element type, also function parameter type
 * @param <U> function result type
 * @return set of results of mapping the function
 */
@Nonnull
public static <T, U> Set<U> setMap(@Nonnull Set<T> xs, @Nonnull Function<T, U> f) {
    HashSet<U> result = new HashSet<U>(xs.size());
    for (T x : xs) {
        result.add(f.apply(x));
    }
    return result;
}

From source file:Main.java

public static final String[] getBondedDeviceNames() {
    BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
    Set<BluetoothDevice> devices = adapter.getBondedDevices();
    String[] names = new String[devices.size()];

    int i = 0;/*  w  w w . j  a v a  2 s.  co m*/
    for (BluetoothDevice d : devices) {
        Log.d(TAG, "Found bonded device " + d.getName());
        names[i++] = d.getName();
    }

    return names;
}

From source file:Main.java

public static boolean saveSet(SharedPreferences prefs, Set<String> set, String setName) {
    SharedPreferences.Editor editor = prefs.edit();
    final int setSize = set.size();
    editor.putInt(setName + "_size", setSize);
    int i = 0;//from w w  w.ja  va 2 s. co  m
    for (String s : set) {
        editor.putString(setName + "_" + i, s);
        ++i;
    }
    return editor.commit();
}

From source file:Main.java

/**
 * Function to remove duplicate elements from the array of string
 * @param stringArray array of string having duplicate elements.
 * @return unique array of string /*from w  w w  .j  a  v a  2s . c o m*/
 */
public static String[] removeDuplicatesFrom(String[] stringArray) {
    Set<String> temp = new HashSet<String>(Arrays.asList(stringArray));
    String[] uniqueStringArray = temp.toArray(new String[temp.size()]);
    return uniqueStringArray;
}

From source file:Main.java

public static <T> Set<T> intersect(Set<T> sourceSet, Set<T> targetSet) {
    Set<T> set = new HashSet<T>();
    int sourceSetSize = sourceSet.size();
    int targetSetSize = targetSet.size();
    Set<T> minSet = null;//from w  w w.  j a va2  s .c  om
    Set<T> maxSet = null;
    if (sourceSetSize <= targetSetSize) {
        minSet = sourceSet;
        maxSet = targetSet;
    } else {
        minSet = targetSet;
        maxSet = sourceSet;

    }

    for (T t : minSet) {
        if (maxSet.contains(t)) {
            set.add(t);
        }
    }
    return set;
}