只要我找不到你们的轮子就不叫重复造轮子 —— 麻了
import java.util.*;
import java.util.function.Function;
/**
* 集合工具类
*
* @author 麻了
*/
public class CollUtilEx {
private CollUtilEx() {
throw new IllegalStateException("Utility class");
}
/**
* 将集合分组
*
* @param coll 集合, 包含多个 item
* @param key 将 item 处理为 key ( 为空则不放分组 )
* @param val 将 item 处理为 val ( 为空则不放分组 )
*/
public static <T, K, V> Map<K, List<V>> group(Collection<T> coll, Function<T, K> key, Function<T, V> val) {
Map<K, List<V>> group = new HashMap<>();
if (Objects.isNull(coll) || coll.isEmpty()) {
return group;
}
for (T item : coll) {
K k = key.apply(item);
V v = val.apply(item);
if (Objects.isNull(k) || Objects.isNull(v)) {
continue;
}
group.putIfAbsent(k, new ArrayList<>());
List<V> thisGroup = group.get(k);
thisGroup.add(v);
}
return group;
}
/**
* 将集合分组
*
* @param coll 集合, 包含多个 item
* @param key 将 item 处理为 key ( 为空则不放分组 )
* @param val 将 item 处理为 val_list, 并将 val_list 的每一项放到当前分组中 ( 为空则不分组 )
*/
public static <T, K, V> Map<K, List<V>> groupValDeep(Collection<T> coll, Function<T, K> key, Function<T, List<V>> val) {
Map<K, List<V>> group = new HashMap<>();
if (Objects.isNull(coll) || coll.isEmpty()) {
return group;
}
for (T item : coll) {
K k = key.apply(item);
List<V> vList = val.apply(item);
if (Objects.isNull(k) || Objects.isNull(vList) || vList.isEmpty()) {
continue;
}
group.putIfAbsent(k, new ArrayList<>());
List<V> thisGroup = group.get(k);
thisGroup.addAll(vList);
}
return group;
}
/**
* 将集合映射
*
* @param coll 集合, 包含多个 item
* @param key 将 item 处理为 key ( 为空则不映射 )
* @param val 将 item 处理为 val ( 为空则不映射 )
*/
public static <T, K, V> Map<K, V> map(Collection<T> coll, Function<T, K> key, Function<T, V> val) {
Map<K, V> map = new HashMap<>();
if (Objects.isNull(coll) || coll.isEmpty()) {
return map;
}
for (T item : coll) {
K k = key.apply(item);
V v = val.apply(item);
if (Objects.isNull(k) || Objects.isNull(v)) {
continue;
}
map.put(k, v);
}
return map;
}
/**
* 将集合映射
*
* @param coll 集合, 包含多个 item
* @param key 将 item 处理为 key_list, 并将每个 key 映射到 val ( 为空则不映射 )
* @param val 将 item 处理为 val ( 为空则不映射 )
*/
public static <T, K, V> Map<K, V> mapKeyDeep(Collection<T> coll, Function<T, List<K>> key, Function<T, V> val) {
Map<K, V> map = new HashMap<>();
if (Objects.isNull(coll) || coll.isEmpty()) {
return map;
}
for (T item : coll) {
List<K> kList = key.apply(item);
V v = val.apply(item);
if (Objects.isNull(kList) || kList.isEmpty() || Objects.isNull(v)) {
continue;
}
for (K k : kList) {
if (Objects.isNull(k)) {
continue;
}
map.put(k, v);
}
}
return map;
}
/**
* 将映射处理成分组
*
* @param key 将 map 的 key 处理为 group 的 key ( 为空则不分组 )
* @param val 将 map 的 val 处理为新的值, 并放到当前分组中 ( 为空则不分组 )
*/
public static <K, V, RK, RV> Map<RK, List<RV>> mapToGroup(Map<K, V> map, Function<K, RK> key, Function<V, RV> val) {
return group(map.entrySet(), item -> key.apply(item.getKey()), item -> val.apply(item.getValue()));
}
/**
* 将数组转换为列表 ( 忽略空项 )
*/
public static List<String> listIgnoreNull(String... items) {
List<String> list = new ArrayList<>();
for (String item : items) {
if (Objects.nonNull(item)) {
list.add(item);
}
}
return list;
}
/**
* 将某个值重复 n 次
*
* @param val 被重复的值
* @param size 重复次数
*/
public static <V> List<V> repeat(V val, int size) {
List<V> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
list.add(val);
}
return list;
}
/**
* 将集合按英文逗号拼接
*
* @param coll 集合, 包含多个 item
* @param str 将 item 处理成 str ( 为空则不拼接 )
*/
public static <T> String join(Collection<T> coll, Function<T, String> str) {
StringBuilder builder = new StringBuilder();
for (T item : coll) {
String itemStr = str.apply(item);
if (Objects.isNull(item) || Objects.isNull(itemStr)) {
continue;
}
builder.append(itemStr).append(",");
}
return builder.deleteCharAt(builder.length() - 1).toString();
}
/**
* 将集合映射到索引值
*
* @param coll 集合, 包含多个 item
* @param key 将 item 处理成 key ( 为空则不映射 )
*/
public static <T, K> Map<K, Integer> toIndexMap(List<T> coll, Function<T, K> key) {
Map<K, Integer> map = new HashMap<>();
if (Objects.isNull(coll) || coll.isEmpty()) {
return map;
}
for (int i = 0; i < coll.size(); i++) {
T item = coll.get(i);
K k = key.apply(item);
if (Objects.isNull(item) || Objects.isNull(k)) {
continue;
}
map.put(k, i);
}
return map;
}
}