引入依赖
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version>4.1</version> </dependency>
工具类:
package ttd.util; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import org.apache.commons.collections4.IterableUtils; import org.apache.commons.collections4.IteratorUtils; import org.apache.commons.collections4.Predicate; import org.apache.commons.collections4.Transformer; import java.util.*; public class CollectionUtil { /** * 判断列表list为空的方法: * Collection一个集合框架来的 也是一个集合接口 通过属性isEmpty()来判断有没有数据 * * @param collection * @return */ public static boolean isEmptyList(Collection<?> collection) { return collection == null || collection.isEmpty(); } /** * 判断Map为空的方法: * Map 是一个键值对映射的集合接口 通过map.keySet()方法来返回一个set再判断它是否为空 * * @param map * @return */ public static boolean isEmptyMap(Map<?, ?> map) { return map == null || isEmptyList(map.keySet()); } /** * Finds the first element in the given iterable which matches the given predicate. * <p> * A <code>null</code> or empty iterator returns null. * * @param <E> the element type * @param iterable the iterable to search, may be null * @param predicate the predicate to use, may not be null * @return the first element of the iterable which matches the predicate or null if none could be found * @throws NullPointerException if predicate is null */ public static <E> E find(Iterable<E> iterable, Predicate<? super E> predicate) { return IterableUtils.find(iterable, predicate); } /** * Answers true if a predicate is true for any element of the iterable. * <p> * A <code>null</code> or empty iterable returns false. * * @param <E> the type of object the {@link Iterable} contains * @param iterable the {@link Iterable} to use, may be null * @param predicate the predicate to use, may not be null * @return true if any element of the collection matches the predicate, false otherwise * @throws NullPointerException if predicate is null */ public static <E> boolean matchesAny(Iterable<E> iterable, Predicate<? super E> predicate) { return IterableUtils.matchesAny(iterable, predicate); } /** * Partitions all elements from iterable into separate output collections, based on the evaluation of the given predicate. * * @param iterable the iterable to partition, may be null * @param predicate the predicate to use, may not be null * @param <O> the type of object the Iterable contains * @return a list containing the output collections * @throws NullPointerException if predicate is null */ public static <O> List<List<O>> partition(Iterable<? extends O> iterable, Predicate<? super O> predicate) { return IterableUtils.partition(iterable, predicate); } /** * 同 partition ,只取条件符合的数据 * * @param iterable * @param predicate * @param <O> * @return */ public static <O> List<O> inPartition(Iterable<? extends O> iterable, Predicate<? super O> predicate) { List<List<O>> lists = IterableUtils.partition(iterable, predicate); if (!isEmptyList(lists)) return lists.get(0); return null; } /** * 同 partition ,只取条件不符合的数据 * * @param iterable * @param predicate * @param <O> * @return */ public static <O> List<O> notInPartition(Iterable<? extends O> iterable, Predicate<? super O> predicate) { List<List<O>> lists = IterableUtils.partition(iterable, predicate); if (!isEmptyList(lists)) return lists.get(1); return null; } /** * Returns a transformed view of the given iterable where all of its elements * have been transformed by the provided transformer. * <p> * The returned iterable's iterator supports {@code remove()} when the corresponding * input iterator supports it. * * @param <I> the input element type * @param <O> the output element type * @param iterable the iterable to transform, may not be null * @param transformer the transformer, must not be null * @return a transformed view of the specified iterable * @throws NullPointerException if either iterable or transformer is null */ public static <I, O> Iterable<O> transformedIterable(final Iterable<I> iterable, final Transformer<? super I, ? extends O> transformer) { if (iterable == null || transformer == null) return null; return IterableUtils.transformedIterable(iterable, transformer); } public static <I, O> List<O> transformedList(final Iterable<I> iterable, final Transformer<? super I, ? extends O> transformer) { Iterable<O> item = transformedIterable(iterable, transformer); if (item == null) return null; return toList(item); } /** * Gets an array based on the iterable. * <p> * As the wrapped Iterator is traversed, an ArrayList of its values is * created. At the end, this is converted to an array. * * @param <E> the element type * @param iterable the iterable to use, not null * @param arrayClass the class of array to create * @return an array of the iterator contents * @throws NullPointerException if iterator parameter or arrayClass is null * @throws ArrayStoreException if the arrayClass is invalid */ public static <E> E[] toArray(Iterable<E> iterable, Class<E> arrayClass) { if (iterable == null || arrayClass == null) return null; return IteratorUtils.toArray(iterable.iterator(), arrayClass); } /** * Gets a new list with the contents of the provided iterable. * * @param <E> the element type * @param iterable the iterable to use, may be null * @return a list of the iterator contents */ public static <E> List<E> toList(Iterable<E> iterable) { if (iterable == null) return null; return IterableUtils.toList(iterable); } /** * Returns a reversed view of the given iterable. * <p> * In case the provided iterable is a {@link List} instance, a * {ReverseListIterator} will be used to reverse the traversal * order, otherwise an intermediate {@link List} needs to be created. * <p> * The returned iterable's iterator supports {@code remove()} if the * provided iterable is a {@link List} instance. * * @param <E> the element type * @param iterable the iterable to use, may not be null * @return a reversed view of the specified iterable * @throws NullPointerException if iterable is null */ public static <E> Iterable<E> reversedIterable(final Iterable<E> iterable) { if (iterable == null) return null; return IterableUtils.reversedIterable(iterable); } /** * 去重,仅支持基本类型,若需要支持自定义的实体,需要重写实体的equals和hashCode方法 * * @param source * @param <E> * @return */ public static <E> List<E> removeDeuplicate(List<E> source) { if (source == null) return null; List<E> result = new ArrayList<>(); for (E s : source) { if (Collections.frequency(result, s) < 1) result.add(s); } return result; } /** * 使用guava工具类来取List集合的差集 * * @param big 大集合 * @param small 小集合 * @return 两个集合的差集 */ public static <O>List<O> getDifference(List<O> big, List<O> small) { Set<O> differenceSet = Sets.difference(Sets.newHashSet(big), Sets.newHashSet(small)); return Lists.newArrayList(differenceSet); } /** * 取集合的第一个参数 * @param iterable * @param <T> * @return */ public static <T> T first(final Iterable<T> iterable) { if (iterable == null || IterableUtils.isEmpty(iterable)) return null; return IterableUtils.get(iterable, 0); } /** * 取集合的最后一个参数 * @param iterable * @param <T> * @return */ public static <T> T last(final Iterable<T> iterable) { if (iterable == null || IterableUtils.isEmpty(iterable)) return null; int size = IterableUtils.size(iterable); return IterableUtils.get(iterable, size - 1); } }