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

cn.micro.core.util.CollectionUtil Maven / Gradle / Ivy

package cn.micro.core.util;

import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Array;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CollectionUtil extends org.springframework.util.CollectionUtils {

    /**
     * 分割list为几个list
     *
     * @param list 源list
     * @param max  分割list大小
     * @param   list 包含对象
     * @return list
     */
    public static  List> splitList(List list, int max) {
        int len = list.size();
        int limit = (len + max - 1) / max;
        return Stream.iterate(0, n -> n + 1)
                .limit(limit).parallel()
                .map(a -> list.stream().skip(a * max).limit(max)
                        .parallel().collect(Collectors.toList()))
                .collect(Collectors.toList());
    }

    /**
     * 合并list
     *
     * @param list 源list
     * @param   list 包含对象
     * @return list
     */
    public static  List combineList(List> list) {
        List newList = list.stream().collect(ArrayList::new, ArrayList::addAll, ArrayList::addAll);
        newList = newList.stream().distinct().collect(Collectors.toList());
        return newList;
    }

    /**
     * 获取list交集
     *
     * @param list1 集合1
     * @param list2 集合2
     * @param    list 包含对象
     * @return list
     */
    public static  List intersectionList(List list1, List list2) {
        return list1.stream()
                .filter(list2::contains)
                .collect(Collectors.toList());
    }

    /**
     * 获取list差集
     *
     * @param list1 集合1
     * @param list2 集合2
     * @param    list 包含对象
     * @return list
     */
    public static  List diffList1ToList2(List list1, List list2) {
        return list1.stream()
                .filter(item -> !list2.contains(item))
                .collect(Collectors.toList());
    }

    /**
     * 数组转 list
     *
     * @param arrays 数组
     * @param     数组对象
     * @return 不可变 list
     */
    @SafeVarargs
    public static  List arrayToNoModifyList(T... arrays) {
        return Arrays.asList(arrays);
    }

    /**
     * 数组转 list
     *
     * @param arrays 数组
     * @param     数组对象
     * @return 可变 list
     */
    @SafeVarargs
    public static  List arrayToList(T... arrays) {
        return Stream.of(arrays).collect(Collectors.toList());
    }


    /**
     * Return {@code true} if the supplied Collection is not {@code null} or empty.
     * Otherwise, return {@code false}.
     *
     * @param collection the Collection to check
     * @return whether the given Collection is not empty
     */
    public static boolean isNotEmpty(@Nullable Collection collection) {
        return !CollectionUtils.isEmpty(collection);
    }

    /**
     * Return {@code true} if the supplied Map is not {@code null} or empty.
     * Otherwise, return {@code false}.
     *
     * @param map the Map to check
     * @return whether the given Map is not empty
     */
    public static boolean isNotEmpty(@Nullable Map map) {
        return !CollectionUtils.isEmpty(map);
    }

    /**
     * Check whether the given Array contains the given element.
     *
     * @param array   the Array to check
     * @param element the element to look for
     * @param      The generic tag
     * @return {@code true} if found, {@code false} else
     */
    public static  boolean contains(@Nullable T[] array, final T element) {
        if (array == null) {
            return false;
        }
        return Arrays.stream(array).anyMatch(x -> ObjectUtils.nullSafeEquals(x, element));
    }

    /**
     * Concatenates 2 arrays
     *
     * @param one   数组1
     * @param other 数组2
     * @return 新数组
     */
    public static String[] concat(String[] one, String[] other) {
        return concat(one, other, String.class);
    }

    /**
     * Concatenates 2 arrays
     *
     * @param one   数组1
     * @param other 数组2
     * @param clazz 数组类
     * @return 新数组
     */
    public static  T[] concat(T[] one, T[] other, Class clazz) {
        T[] target = (T[]) Array.newInstance(clazz, one.length + other.length);
        System.arraycopy(one, 0, target, 0, one.length);
        System.arraycopy(other, 0, target, one.length, other.length);
        return target;
    }

    /**
     * 不可变 Set
     *
     * @param es  对象
     * @param  泛型
     * @return 集合
     */
    @SafeVarargs
    public static  Set ofImmutableSet(E... es) {
        return Arrays.stream(Objects.requireNonNull(es, "args es is null.")).collect(Collectors.toSet());
    }

    /**
     * 不可变 List
     *
     * @param es  对象
     * @param  泛型
     * @return 集合
     */
    @SafeVarargs
    public static  List ofImmutableList(E... es) {
        return Arrays.stream(Objects.requireNonNull(es, "args es is null.")).collect(Collectors.toList());
    }

    /**
     * Iterable 转换为List集合
     *
     * @param elements Iterable
     * @param       泛型
     * @return 集合
     */
    public static  List toList(Iterable elements) {
        Objects.requireNonNull(elements, "elements es is null.");
        if (elements instanceof Collection) {
            return new ArrayList<>((Collection) elements);
        }
        Iterator iterator = elements.iterator();
        List list = new ArrayList<>();
        while (iterator.hasNext()) {
            list.add(iterator.next());
        }
        return list;
    }

    /**
     * 将key value 数组转为 map
     *
     * @param keysValues key value 数组
     * @param         key
     * @param         value
     * @return map 集合
     */
    public static  Map toMap(Object... keysValues) {
        int kvLength = keysValues.length;
        if (kvLength % 2 != 0) {
            throw new IllegalArgumentException("wrong number of arguments for met, keysValues length can not be odd");
        }
        Map keyValueMap = new HashMap<>(kvLength);
        for (int i = kvLength - 2; i >= 0; i -= 2) {
            Object key = keysValues[i];
            Object value = keysValues[i + 1];
            keyValueMap.put((K) key, (V) value);
        }
        return keyValueMap;
    }

    /**
     * A temporary workaround for Java 8 specific performance issue JDK-8161372 .
* This class should be removed once we drop Java 8 support. * * @see https://bugs.openjdk.java.net/browse/JDK-8161372 */ public static V computeIfAbsent(Map map, K key, Function mappingFunction) { V value = map.get(key); if (value != null) { return value; } return map.computeIfAbsent(key, mappingFunction); } /** * list 分片 * * @param list List * @param size 分片大小 * @param 泛型 * @return List 分片 */ public static List> partition(List list, int size) { Objects.requireNonNull(list, "List to partition must not null."); Assert.isTrue(size > 0, "List to partition size must more then zero."); return (list instanceof RandomAccess) ? new RandomAccessPartition<>(list, size) : new Partition<>(list, size); } private static class RandomAccessPartition extends Partition implements RandomAccess { RandomAccessPartition(List list, int size) { super(list, size); } } private static class Partition extends AbstractList> { final List list; final int size; Partition(List list, int size) { this.list = list; this.size = size; } @Override public List get(int index) { if (index >= 0 && index < this.size()) { int start = index * this.size; int end = Math.min(start + this.size, this.list.size()); return this.list.subList(start, end); } throw new IndexOutOfBoundsException(String.format("index (%s) must be less than size (%s)", index, this.size())); } @Override public int size() { return ceilDiv(this.list.size(), this.size); } @Override public boolean isEmpty() { return this.list.isEmpty(); } private static int ceilDiv(int x, int y) { int r = x / y; if (r * y < x) { r++; } return r; } } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy