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

cn.keayuan.util.CollectionUtils Maven / Gradle / Ivy

The newest version!
package cn.keayuan.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import cn.keayuan.util.function.BiConsumer;
import cn.keayuan.util.function.Consumer;
import cn.keayuan.util.function.Predicate;

/**
 * Created by keayuan on 2021/2/4.
 *
 * @author keayuan
 */
public class CollectionUtils {

    private CollectionUtils() {
    }

    public static boolean isEmpty(Collection collection) {
        return collection == null || collection.isEmpty();
    }

    public static boolean isNotEmpty(Collection collection) {
        return !isEmpty(collection);
    }

    public static  void forEach(Iterable iterable, Consumer consumer) {
        if (consumer == null) throw new NullPointerException();
        if (iterable != null) {
            forEach(iterable.iterator(), consumer);
        }
    }

    public static  void forEach(Iterator iterator, Consumer consumer) {
        if (consumer == null) throw new NullPointerException();
        if (iterator == null) return;
        while (iterator.hasNext()) consumer.accept(iterator.next());
    }

    //================== List ==================
    public static  T getLast(List collection) {
        if (isEmpty(collection)) return null;
        int size = collection.size();
        return collection.get(size - 1);
    }

    public static  T getFirst(List collection) {
        if (isEmpty(collection)) return null;
        return collection.get(0);
    }

    public static int getSize(List list) {
        return list == null ? 0 : list.size();
    }

    public static  T getQuietly(List list, int position) {
        if (position < 0 || position >= getSize(list)) return null;
        return list.get(position);
    }

    public static  void forEach(List list, BiConsumer consumer) {
        if (consumer == null) throw new NullPointerException();
        if (isEmpty(list)) {
            return;
        }
        int size = getSize(list);
        for (int i = 0; i < size; i++) {
            consumer.accept(i, list.get(i));
        }
    }

    public static  E findFirst(List list, Predicate predicate) {
        if (predicate == null) throw new NullPointerException();
        if (isEmpty(list)) {
            return null;
        }
        for (E e : list) {
            if (predicate.test(e)) {
                return e;
            }
        }
        return null;
    }

    public static  List filter(List list, Predicate predicate) {
        if (predicate == null) throw new NullPointerException();
        List ret = new ArrayList<>();
        for (E e : list) {
            if (predicate.test(e)) {
                ret.add(e);
            }
        }
        return ret;
    }

    //================== Array ==================
    public static  boolean isEmpty(T[] arr) {
        return arr == null || arr.length == 0;
    }

    public static  boolean isNotEmpty(T[] arr) {
        return !isEmpty(arr);
    }

    public static  int getSize(T[] arr) {
        return arr == null ? 0 : arr.length;
    }

    public static  E getFirst(E[] arr) {
        return isEmpty(arr) ? null : arr[0];
    }

    public static  E getLast(E[] arr) {
        return isEmpty(arr) ? null : arr[arr.length - 1];
    }

    public static  void forEach(E[] arr, Consumer consumer) {
        if (consumer == null) throw new NullPointerException();
        if (isEmpty(arr)) {
            return;
        }
        for (E e : arr) {
            consumer.accept(e);
        }
    }

    public static  void forEach(E[] arr, BiConsumer consumer) {
        if (consumer == null) throw new NullPointerException();
        if (isEmpty(arr)) {
            return;
        }
        int size = getSize(arr);
        for (int i = 0; i < size; i++) {
            consumer.accept(i, arr[i]);
        }
    }

    //================== Map ==================

    public static boolean isEmpty(Map map) {
        return map == null || map.size() == 0;
    }

    public static boolean isNotEmpty(Map map) {
        return !isEmpty(map);
    }

    public static  void forEach(Map map, BiConsumer consumer) {
        if (consumer == null) throw new NullPointerException();
        if (map == null) return;
        for (Map.Entry entry : map.entrySet()) {
            consumer.accept(entry.getKey(), entry.getValue());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy