Example usage for java.util Set toArray

List of usage examples for java.util Set toArray

Introduction

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

Prototype

<T> T[] toArray(T[] a);

Source Link

Document

Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.

Usage

From source file:Main.java

public static String[] union(String[] arr1, String[] arr2) {
    Set<String> set = new HashSet<>();
    Collections.addAll(set, arr1);
    Collections.addAll(set, arr2);
    String[] result = {};//from w ww .j a v a  2  s .c  o  m
    return set.toArray(result);
}

From source file:Main.java

/**
 * Returns all the unique fields of the classes in the given list.
 * @author Tristan Bepler//from ww  w.jav a  2s.  c om
 */
private static Field[] getAllFields(List<Class<?>> classes) {
    Set<Field> fields = new HashSet<Field>();
    for (Class<?> clazz : classes) {
        fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
    }
    return fields.toArray(new Field[fields.size()]);
}

From source file:Main.java

public static CharSequence[] getDeviceNames(@Nullable Set<BluetoothDevice> bluetoothDevices) {
    if (bluetoothDevices == null) {
        return new CharSequence[0];
    }//from   w  w  w  . j  a v a  2  s  .  co  m

    BluetoothDevice[] devices = bluetoothDevices.toArray(new BluetoothDevice[bluetoothDevices.size()]);
    CharSequence[] deviceNames = new CharSequence[devices.length];

    for (int i = 0; i < deviceNames.length; i++) {
        deviceNames[i] = devices[i].getName();
    }

    return deviceNames;
}

From source file:Main.java

/**
 * Return a sorted array of holidays for a given year.
 *
 * @param year//w  w  w  .jav a  2s .c om
 *            The year to get holidays for.
 * @return The array of holidays, sorted by date.
 */
public static Date[] getHolidays(int year) {
    Set<Date> days = getHolidaySet(year);
    Date[] dates = days.toArray(new Date[days.size()]);
    Arrays.sort(dates);
    return dates;
}

From source file:com.esofthead.mycollab.cache.LocalCacheManager.java

static void removeCacheItems(String id, String prefixKey) {
    BasicCache<String, Object> cache = instance.getCache(id);
    LOG.debug("Remove cache has prefix {} in group {}", prefixKey, id);
    Set<String> keys = cache.keySet();
    if (CollectionUtils.isNotEmpty(keys)) {

        String[] keyArr = keys.toArray(new String[0]);
        for (int i = 0; i < keyArr.length; i++) {
            if (keyArr[i].startsWith(prefixKey)) {
                LOG.debug("Remove cache key {}", keyArr[i]);
                cache.remove(keyArr[i]);
            }// ww w . ja v a  2  s .c  o m
        }
    }
}

From source file:com.h6ah4i.android.compat.utils.SharedPreferencesJsonStringSetWrapperUtils.java

public static boolean putStringSet(SharedPreferences.Editor editor, String key, Set<String> values) {
    try {//from   ww  w. j  ava 2s  . c o m
        final JSONArray a = new JSONArray();
        final String[] strValues = (String[]) values.toArray(new String[values.size()]);

        for (int i = 0; i < values.size(); i++) {
            a.put(strValues[i]);
        }

        if (!values.isEmpty()) {
            editor.putString(key, a.toString());
        } else {
            editor.putString(key, null);
        }

        return editor.commit();
    } catch (RuntimeException e) {
        Log.e(TAG, "putStringSet()", e);

        return false;
    }
}

From source file:org.springmodules.util.Strings.java

/**
 * Removes duplicates from the given String array.
 * // w w w. j  av a  2s .com
 * @param array
 *          the given array
 * @return a new String array without duplicated entries
 */
public static String[] removeDuplicates(String[] array) {
    if (ObjectUtils.isEmpty(array)) {
        return array;
    }
    Set set = new TreeSet();

    int arraySize = array.length;
    for (int i = 0; i < arraySize; i++) {
        set.add(array[i]);
    }
    return (String[]) set.toArray(new String[set.size()]);
}

From source file:Main.java

public static String[] union(String[] arr1, String[] arr2) {
    Set<String> set = new HashSet<>();
    for (String str : arr1) {
        set.add(str);//  w ww.j  a v a  2 s  .co m
    }
    for (String str : arr2) {
        set.add(str);
    }
    String[] result = {};
    return set.toArray(result);
}

From source file:Main.java

/**
 * As {@link #getAllFields(Class)} but acts on a list of {@link Class}s and
 * uses only {@link Class#getDeclaredFields()}.
 *
 * @param classes The list of classes to reflect on
 * @return The complete list of fields/*ww w  .j av a 2  s.  co m*/
 */
private static Field[] getAllFields(List<Class<?>> classes) {
    Set<Field> fields = new HashSet<Field>();
    for (Class<?> clazz : classes) {
        fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
    }

    return fields.toArray(new Field[fields.size()]);
}

From source file:co.turnus.analysis.util.AnalysisUtil.java

public static int[] linspacei(int min, int max) {
    Set<Integer> values = new LinkedHashSet<Integer>();
    for (int i = min; i <= max; i++) {
        values.add(i);/*from  w  w w .j a  v  a 2  s  .c  o  m*/
    }
    return ArrayUtils.toPrimitive(values.toArray(new Integer[0]));
}