package com.yashi.common.utils;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import javafx.util.Pair;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;
/** 集合类工具 **/
public final class CollectsHelper {
private CollectsHelper() {
}
// 集合
public static <T> boolean isNullOrEmpty(final Collection<T> collection) {
return collection == null || collection.isEmpty();
}
// 数组
public static <T> boolean isNullOrEmpty(final T[] array) {
return array == null || array.length == 0;
}
// map
public static <K, V> boolean isNullOrEmpty(final Map<K, V> map) {
return map == null || map.isEmpty();
}
// 是否包含某元素
public static <T> boolean contains(final T[] array, final T val) {
for (T t : array) {
if (val.equals(t)) {
return true;
}
}
return false;
}
public static <T> Optional<T> head(final List<T> list) {
return isNullOrEmpty(list) ? Optional.empty() : Optional.ofNullable(list.get(0));
}
public static <T> Optional<T> end(final List<T> list) {
return isNullOrEmpty(list) ? Optional.empty() : Optional.ofNullable(list.get(list.size() - 1));
}
public static <T, R> List<R> map(final Collection<T> list, Function<? super T, R> mapF) {
return isNullOrEmpty(list) ? Lists.newArrayList() : list.stream().map(mapF).collect(Collectors.toList());
}
public static <T> List<T> filter(final List<T> list, Function<T, Boolean> compare) {
if(null == compare) {
return list;
}
if(!isNullOrEmpty(list)) {
return list.stream().filter(t -> compare.apply(t)).collect(Collectors.toList());
}
return Lists.newArrayList();
}
public static <V> Map<Integer, V> asMap(final List<V> list) {
Map<Integer, V> result = Maps.newHashMap();
if(!isNullOrEmpty(list)) {
int size = list.size();
for (int idx = 0; idx < size; idx++) {
result.put(idx, list.get(idx));
}
}
return result;
}
public static <K, V> LinkedHashMap<K, V> ofMap(final List<V> list, final Function<V, K> keyF) {
LinkedHashMap<K, V> result = Maps.newLinkedHashMap();
if(!isNullOrEmpty(list)) {
for (V value : list) {
result.put(keyF.apply(value), value);
}
}
return result;
}
public static <K, V, R> ListMultimap<K, R> group(final List<V> list, final Function<V, Pair<K, R>> pairF) {
ListMultimap<K, R> result = ArrayListMultimap.create();
if(!isNullOrEmpty(list)) {
for (V v : list) {
Pair<K, R> pair = pairF.apply(v);
result.put(pair.getKey(), pair.getValue());
}
}
return result;
}
public static <T, K, U, A, R> Map<K, R> groupToCollector(final Collection<T> list,
final Function<? super T, K> keyFunc,
final Function<? super T, ? extends U> groupByEle,
final Collector<? super U, A, R> valueCol) {
if(CollectsHelper.isNullOrEmpty(list)){
return Maps.newHashMap();
}
return list.stream().collect(Collectors
.groupingBy(keyFunc,Collectors.mapping(groupByEle, valueCol)));
}
public static void main(String[] args) {
Map map=new HashMap();
// map.put("1","00");
boolean b = CollectsHelper.isNullOrEmpty(map);
System.out.println("b = " + b);
}
}
标签:return,list,public,static,集合,工具,isNullOrEmpty,final From: https://www.cnblogs.com/cjxiaozhuang/p/17475060.html