All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dongliu.commons.collection.Collections2 Maven / Gradle / Ivy

There is a newer version: 6.7.0
Show newest version
package net.dongliu.commons.collection;

import javax.annotation.Nullable;
import java.util.*;
import java.util.function.Function;

/**
 * Utils method for collection
 *
 * @author Liu Dong
 */
public class Collections2 {

    /**
     * If collection is null, return immutable empty collection; else return self
     */
    public static  Collection nullToEmpty(@Nullable Collection c) {
        return c == null ? Lists.of() : c;
    }

    /**
     * wrap to immutable collection
     */
    public static  Collection wrapImmutable(Collection collection) {
        return Collections.unmodifiableCollection(collection);
    }

    /**
     * Convert to int array
     */
    public static int[] toIntArray(Collection c) {
        if (c.isEmpty()) {
            return Arrays2.EMPTY_INT_ARRAY;
        }
        int[] array = new int[c.size()];
        int idx = 0;
        for (Integer integer : c) {
            array[idx++] = integer;
        }
        return array;
    }

    /**
     * Convert to long array
     */
    public static long[] toLongArray(Collection c) {
        if (c.isEmpty()) {
            return Arrays2.EMPTY_LONG_ARRAY;
        }
        long[] array = new long[c.size()];
        int idx = 0;
        for (Long value : c) {
            array[idx++] = value;
        }
        return array;
    }

    /**
     * Convert to String array
     */
    public static String[] toStringArray(Collection c) {
        if (c.isEmpty()) {
            return Arrays2.EMPTY_STRING_ARRAY;
        }
        return toArray(c, String.class);
    }

    /**
     * Convert collection to array
     *
     * @param c not null
     */
    public static  T[] toArray(Collection c, Class cls) {
        T[] array = Arrays2.newArray(c.size(), cls);
        int i = 0;
        for (T value : c) {
            array[i++] = value;
        }
        return array;
    }

    /**
     * Convert collection
     */
    public static  Collection convert(Collection collection, Function function) {
        List list = new ArrayList<>(collection.size());
        for (T value : collection) {
            list.add(function.apply(value));
        }
        return list;
    }

    /**
     * Convert collection to map
     */
    public static  Map toMap(Collection list, Function keyMapper,
                                            Function valueMapper) {
        Map map = new HashMap<>();
        for (T value : list) {
            map.put(keyMapper.apply(value), valueMapper.apply(value));
        }
        return map;
    }

    /**
     * Convert collection to map
     */
    public static  Map toMap(Collection list, Function keyMapper) {
        Map map = new HashMap<>();
        for (T value : list) {
            map.put(keyMapper.apply(value), value);
        }
        return map;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy