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

net.dongliu.commons.collection.Lists 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;
import java.util.function.Predicate;
import java.util.function.Supplier;

/**
 * List utils functions
 *
 * @author Liu Dong
 */
public class Lists {

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

    /**
     * Transform list
     */
    public static  List convert(Collection list, Function function) {
        return convert(() -> new ArrayList<>(list.size()), list, function);
    }

    /**
     * Transform list
     */
    public static > E convert(Supplier supplier, Collection list,
                                                      Function function) {
        E newList = supplier.get();
        for (T value : list) {
            newList.add(function.apply(value));
        }
        return newList;
    }

    /**
     * Get new list with items accepted by predicate
     */
    public static  List filter(List list, Predicate predicate) {
        return filter(() -> new ArrayList<>(list.size()), list, predicate);
    }

    /**
     * Get new list with items accepted by predicate
     */
    public static > E filter(Supplier supplier, List list, Predicate predicate) {
        E newList = supplier.get();
        for (T value : list) {
            if (predicate.test(value)) {
                newList.add(value);
            }
        }
        return newList;
    }

    /**
     * Convert collection to map
     */
    public static  Map toMap(List list, Function keyMapper,
                                            Function valueMapper) {
        return toMap(() -> new HashMap<>((int) (list.size() / 0.75f) + 1, 0.75f), list, keyMapper, valueMapper);
    }

    /**
     * Convert collection to map
     */
    public static > E toMap(Supplier supplier, List list,
                                                         Function keyMapper,
                                                         Function valueMapper) {
        E map = supplier.get();
        for (T value : list) {
            map.put(keyMapper.apply(value), valueMapper.apply(value));
        }
        return map;
    }

    /**
     * Convert collection to map
     */
    public static  Map toMap(List list, Function keyMapper) {
        return toMap(() -> new HashMap<>((int) (list.size() / 0.75f) + 1, 0.75f), list, keyMapper);
    }

    /**
     * Convert collection to map
     */
    public static > E toMap(Supplier supplier,
                                                      List list, Function keyMapper) {
        E map = supplier.get();
        for (T value : list) {
            map.put(keyMapper.apply(value), value);
        }
        return map;
    }

    /**
     * Create immutable view of this List.
     */
    public static  List wrapImmutable(List list) {
        return Collections.unmodifiableList(list);
    }

    /**
     * Create a immutable list with content
     */
    public static  List copyImmutable(Collection list) {
        return Collections.unmodifiableList(new ArrayList<>(list));
    }

    /**
     * Create mutable list
     */
    public static  List create() {
        return new ArrayList<>();
    }

    /**
     * Create mutable list
     */
    public static  List create(T value) {
        List list = new ArrayList<>(1);
        list.add(value);
        return list;
    }

    /**
     * Create mutable list
     */
    public static  List create(T value1, T value2) {
        List list = new ArrayList<>(2);
        list.add(value1);
        list.add(value2);
        return list;
    }

    /**
     * Create mutable list
     */
    public static  List create(T value1, T value2, T value3) {
        List list = new ArrayList<>(3);
        list.add(value1);
        list.add(value2);
        list.add(value3);
        return list;
    }

    /**
     * Create mutable list
     */
    public static  List create(T value1, T value2, T value3, T value4) {
        List list = new ArrayList<>(4);
        list.add(value1);
        list.add(value2);
        list.add(value3);
        list.add(value4);
        return list;
    }

    /**
     * Create mutable list
     */
    public static  List create(T value1, T value2, T value3, T value4, T value5) {
        List list = new ArrayList<>(5);
        list.add(value1);
        list.add(value2);
        list.add(value3);
        list.add(value4);
        list.add(value5);
        return list;
    }

    /**
     * Create mutable list
     */
    public static  List create(T value1, T value2, T value3, T value4, T value5, T value6) {
        List list = new ArrayList<>(6);
        list.add(value1);
        list.add(value2);
        list.add(value3);
        list.add(value4);
        list.add(value5);
        list.add(value6);
        return list;
    }

    /**
     * Create mutable list
     */
    public static  List create(T value1, T value2, T value3, T value4, T value5, T value6, T value7) {
        List list = new ArrayList<>(7);
        list.add(value1);
        list.add(value2);
        list.add(value3);
        list.add(value4);
        list.add(value5);
        list.add(value6);
        list.add(value7);
        return list;
    }

    /**
     * create mutable list
     */
    @SafeVarargs
    public static  List create(T... values) {
        List list = new ArrayList<>(values.length);
        Collections.addAll(list, values);
        return list;
    }


    /**
     * Create immutable list
     */
    public static  List of() {
        return Collections.emptyList();
    }

    /**
     * Create immutable list
     */
    public static  List of(T value) {
        return Collections.singletonList(value);
    }

    /**
     * create immutable list
     */
    @SafeVarargs
    public static  List of(T... values) {
        return Collections.unmodifiableList(Arrays.asList(values));
    }

    /**
     * Split list into pieces by fix size
     *
     * @param subSize should be larger than 0
     */
    public static  List> split(List list, int subSize) {
        int len = list.size();
        int count = (len - 1) / subSize + 1;
        List> result = new ArrayList<>(count);
        for (int i = 0; i < count; i++) {
            int start = i * subSize;
            List subList = list.subList(start, Math.max(start + subSize, len));
            result.add(subList);
        }
        return result;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy