mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
Merge branch 'v5-dev' of gitee.com:dromara/hutool into v5-dev
This commit is contained in:
@@ -2,6 +2,7 @@ package cn.hutool.core.collection;
|
||||
|
||||
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.stream.StreamUtil;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.BiFunction;
|
||||
@@ -27,10 +28,27 @@ public class CollStreamUtil {
|
||||
* @return 转化后的map
|
||||
*/
|
||||
public static <V, K> Map<K, V> toIdentityMap(Collection<V> collection, Function<V, K> key) {
|
||||
return toIdentityMap(collection, key, false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将collection转化为类型不变的map<br>
|
||||
* <B>{@code Collection<V> ----> Map<K,V>}</B>
|
||||
*
|
||||
* @param collection 需要转化的集合
|
||||
* @param key V类型转化为K类型的lambda方法
|
||||
* @param isParallel 是否并行流
|
||||
* @param <V> collection中的泛型
|
||||
* @param <K> map中的key类型
|
||||
* @return 转化后的map
|
||||
*/
|
||||
public static <V, K> Map<K, V> toIdentityMap(Collection<V> collection, Function<V, K> key, boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return collection.stream().collect(Collectors.toMap(key, Function.identity(), (l, r) -> l));
|
||||
return StreamUtil.of(collection, isParallel)
|
||||
.collect(Collectors.toMap(key, Function.identity(), (l, r) -> l));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,12 +64,27 @@ public class CollStreamUtil {
|
||||
* @return 转化后的map
|
||||
*/
|
||||
public static <E, K, V> Map<K, V> toMap(Collection<E> collection, Function<E, K> key, Function<E, V> value) {
|
||||
return toMap(collection, key, value, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param collection 需要转化的集合
|
||||
* @param key E类型转化为K类型的lambda方法
|
||||
* @param value E类型转化为V类型的lambda方法
|
||||
* @param isParallel 是否并行流
|
||||
* @param <E> collection中的泛型
|
||||
* @param <K> map中的key类型
|
||||
* @param <V> map中的value类型
|
||||
* @return 转化后的map
|
||||
*/
|
||||
public static <E, K, V> Map<K, V> toMap(Collection<E> collection, Function<E, K> key, Function<E, V> value, boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return collection.stream().collect(Collectors.toMap(key, value, (l, r) -> l));
|
||||
return StreamUtil.of(collection, isParallel).collect(Collectors.toMap(key, value, (l, r) -> l));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将collection按照规则(比如有相同的班级id)分类成map<br>
|
||||
* <B>{@code Collection<E> -------> Map<K,List<E>> } </B>
|
||||
@@ -63,12 +96,25 @@ public class CollStreamUtil {
|
||||
* @return 分类后的map
|
||||
*/
|
||||
public static <E, K> Map<K, List<E>> groupByKey(Collection<E> collection, Function<E, K> key) {
|
||||
return groupByKey(collection, key, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将collection按照规则(比如有相同的班级id)分类成map<br>
|
||||
* <B>{@code Collection<E> -------> Map<K,List<E>> } </B>
|
||||
*
|
||||
* @param collection 需要分类的集合
|
||||
* @param key 分类的规则
|
||||
* @param isParallel 是否并行流
|
||||
* @param <E> collection中的泛型
|
||||
* @param <K> map中的key类型
|
||||
* @return 分类后的map
|
||||
*/
|
||||
public static <E, K> Map<K, List<E>> groupByKey(Collection<E> collection, Function<E, K> key, boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return collection
|
||||
.stream()
|
||||
.collect(Collectors.groupingBy(key, Collectors.toList()));
|
||||
return StreamUtil.of(collection, isParallel).collect(Collectors.groupingBy(key, Collectors.toList()));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,11 +130,29 @@ public class CollStreamUtil {
|
||||
* @return 分类后的map
|
||||
*/
|
||||
public static <E, K, U> Map<K, Map<U, List<E>>> groupBy2Key(Collection<E> collection, Function<E, K> key1, Function<E, U> key2) {
|
||||
return groupBy2Key(collection, key1, key2, false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将collection按照两个规则(比如有相同的年级id,班级id)分类成双层map<br>
|
||||
* <B>{@code Collection<E> ---> Map<T,Map<U,List<E>>> } </B>
|
||||
*
|
||||
* @param collection 需要分类的集合
|
||||
* @param key1 第一个分类的规则
|
||||
* @param key2 第二个分类的规则
|
||||
* @param isParallel 是否并行流
|
||||
* @param <E> 集合元素类型
|
||||
* @param <K> 第一个map中的key类型
|
||||
* @param <U> 第二个map中的key类型
|
||||
* @return 分类后的map
|
||||
*/
|
||||
public static <E, K, U> Map<K, Map<U, List<E>>> groupBy2Key(Collection<E> collection, Function<E, K> key1,
|
||||
Function<E, U> key2, boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return collection
|
||||
.stream()
|
||||
return StreamUtil.of(collection, isParallel)
|
||||
.collect(Collectors.groupingBy(key1, Collectors.groupingBy(key2, Collectors.toList())));
|
||||
}
|
||||
|
||||
@@ -105,11 +169,28 @@ public class CollStreamUtil {
|
||||
* @return 分类后的map
|
||||
*/
|
||||
public static <E, T, U> Map<T, Map<U, E>> group2Map(Collection<E> collection, Function<E, T> key1, Function<E, U> key2) {
|
||||
return group2Map(collection, key1, key2, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将collection按照两个规则(比如有相同的年级id,班级id)分类成双层map<br>
|
||||
* <B>{@code Collection<E> ---> Map<T,Map<U,E>> } </B>
|
||||
*
|
||||
* @param collection 需要分类的集合
|
||||
* @param key1 第一个分类的规则
|
||||
* @param key2 第二个分类的规则
|
||||
* @param isParallel 是否并行流
|
||||
* @param <T> 第一个map中的key类型
|
||||
* @param <U> 第二个map中的key类型
|
||||
* @param <E> collection中的泛型
|
||||
* @return 分类后的map
|
||||
*/
|
||||
public static <E, T, U> Map<T, Map<U, E>> group2Map(Collection<E> collection,
|
||||
Function<E, T> key1, Function<E, U> key2, boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection) || key1 == null || key2 == null) {
|
||||
return Collections.emptyMap();
|
||||
}
|
||||
return collection
|
||||
.stream()
|
||||
return StreamUtil.of(collection, isParallel)
|
||||
.collect(Collectors.groupingBy(key1, Collectors.toMap(key2, Function.identity(), (l, r) -> l)));
|
||||
}
|
||||
|
||||
@@ -124,11 +205,25 @@ public class CollStreamUtil {
|
||||
* @return 转化后的list
|
||||
*/
|
||||
public static <E, T> List<T> toList(Collection<E> collection, Function<E, T> function) {
|
||||
return toList(collection, function, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将collection转化为List集合,但是两者的泛型不同<br>
|
||||
* <B>{@code Collection<E> ------> List<T> } </B>
|
||||
*
|
||||
* @param collection 需要转化的集合
|
||||
* @param function collection中的泛型转化为list泛型的lambda表达式
|
||||
* @param isParallel 是否并行流
|
||||
* @param <E> collection中的泛型
|
||||
* @param <T> List中的泛型
|
||||
* @return 转化后的list
|
||||
*/
|
||||
public static <E, T> List<T> toList(Collection<E> collection, Function<E, T> function, boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return collection
|
||||
.stream()
|
||||
return StreamUtil.of(collection, isParallel)
|
||||
.map(function)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toList());
|
||||
@@ -145,11 +240,25 @@ public class CollStreamUtil {
|
||||
* @return 转化后的Set
|
||||
*/
|
||||
public static <E, T> Set<T> toSet(Collection<E> collection, Function<E, T> function) {
|
||||
if (CollUtil.isEmpty(collection) || function == null) {
|
||||
return toSet(collection, function, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将collection转化为Set集合,但是两者的泛型不同<br>
|
||||
* <B>{@code Collection<E> ------> Set<T> } </B>
|
||||
*
|
||||
* @param collection 需要转化的集合
|
||||
* @param function collection中的泛型转化为set泛型的lambda表达式
|
||||
* @param isParallel 是否并行流
|
||||
* @param <E> collection中的泛型
|
||||
* @param <T> Set中的泛型
|
||||
* @return 转化后的Set
|
||||
*/
|
||||
public static <E, T> Set<T> toSet(Collection<E> collection, Function<E, T> function, boolean isParallel) {
|
||||
if (CollUtil.isEmpty(collection)) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
return collection
|
||||
.stream()
|
||||
return StreamUtil.of(collection, isParallel)
|
||||
.map(function)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
|
@@ -12,12 +12,16 @@ import java.util.Map;
|
||||
* 资源类加载器,可以加载任意类型的资源类
|
||||
*
|
||||
* @param <T> {@link Resource}接口实现类
|
||||
* @author looly
|
||||
* @author looly, lzpeng
|
||||
* @since 5.5.2
|
||||
*/
|
||||
public class ResourceClassLoader<T extends Resource> extends SecureClassLoader {
|
||||
|
||||
private final Map<String, T> resourceMap;
|
||||
/**
|
||||
* 缓存已经加载的类
|
||||
*/
|
||||
private final Map<String, Class<?>> cacheClassMap;
|
||||
|
||||
/**
|
||||
* 构造
|
||||
@@ -28,25 +32,42 @@ public class ResourceClassLoader<T extends Resource> extends SecureClassLoader {
|
||||
public ResourceClassLoader(ClassLoader parentClassLoader, Map<String, T> resourceMap) {
|
||||
super(ObjectUtil.defaultIfNull(parentClassLoader, ClassLoaderUtil.getClassLoader()));
|
||||
this.resourceMap = ObjectUtil.defaultIfNull(resourceMap, new HashMap<>());
|
||||
this.cacheClassMap = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加需要加载的类资源
|
||||
*
|
||||
* @param resource 资源,可以是文件、流或者字符串
|
||||
* @return this
|
||||
*/
|
||||
public ResourceClassLoader<T> addResource(T resource){
|
||||
public ResourceClassLoader<T> addResource(T resource) {
|
||||
this.resourceMap.put(resource.getName(), resource);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Class<?> findClass(String name) throws ClassNotFoundException {
|
||||
final Class<?> clazz = cacheClassMap.computeIfAbsent(name, this::defineByName);
|
||||
if (clazz == null) {
|
||||
return super.findClass(name);
|
||||
}
|
||||
return clazz;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从给定资源中读取class的二进制流,然后生成类<br>
|
||||
* 如果这个类资源不存在,返回{@code null}
|
||||
*
|
||||
* @param name 类名
|
||||
* @return 定义的类
|
||||
*/
|
||||
private Class<?> defineByName(String name) {
|
||||
final Resource resource = resourceMap.get(name);
|
||||
if (null != resource) {
|
||||
final byte[] bytes = resource.readBytes();
|
||||
return defineClass(name, bytes, 0, bytes.length);
|
||||
}
|
||||
return super.findClass(name);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user