Collections集合工具类
发布人:shili8
发布时间:2024-11-18 15:53
阅读次数:0
**Collections集合工具类**
在Java开发中,集合框架是非常重要的一部分。它提供了各种类型的集合,如List、Set、Map等,使我们能够存储和操作数据。在实际项目中,我们经常需要对集合进行一些通用的操作,如遍历、排序、过滤等。为了提高代码的重用性和可读性,通常会创建一个工具类来封装这些集合相关的方法。
下面是Collections集合工具类的实现:
javaimport java.util.*;
public class CollectionsUtil {
/**
* 判断集合是否为空 *
* @param collection 集合 * @return true:空 false:非空 */
public static boolean isEmpty(Collection> collection) {
return collection == null || collection.isEmpty();
}
/**
* 判断Map是否为空 *
* @param map Map * @return true:空 false:非空 */
public static boolean isEmpty(Map, ?> map) {
return map == null || map.isEmpty();
}
/**
* 获取集合的大小 *
* @param collection 集合 * @return 大小 */
public static int size(Collection> collection) {
return collection != null ? collection.size() :0;
}
/**
* 获取Map的大小 *
* @param map Map * @return 大小 */
public static int size(Map, ?> map) {
return map != null ? map.size() :0;
}
/**
* 将集合转换为数组 *
* @param collection 集合 * @param 元素类型 * @return 数组 */
public static T[] toArray(Collection collection) {
return collection != null ? collection.toArray((T[]) new Object[0]) : null;
}
/**
* 将Map转换为List *
* @param map Map * @return List */
public static List> toList(Map, ?> map) {
return map != null ? new ArrayList<>(map.entrySet()) : null;
}
/**
* 过滤集合中的元素 *
* @param collection 集合 * @param predicate 过滤条件 * @return 过滤后的集合 */
public static Collection filter(Collection collection, Predicate predicate) {
return collection != null ? new ArrayList<>(filter(collection.iterator(), predicate)) : null;
}
/**
* 过滤Map中的元素 *
* @param map Map * @param predicate 过滤条件 * @return 过滤后的Map */
public static Map filter(Map map, Predicate> predicate) {
return map != null ? new HashMap<>() {{
for (Map.Entry entry : map.entrySet()) {
if (predicate.test(entry)) {
put(entry.getKey(), entry.getValue());
}
}
}} : null;
}
/**
* 将集合中的元素排序 *
* @param collection 集合 * @param comparator 比较器 */
public static void sort(Collection collection, Comparator comparator) {
if (collection != null && comparator != null) {
List list = new ArrayList<>(collection);
Collections.sort(list, comparator);
collection.clear();
collection.addAll(list);
}
}
/**
* 将Map中的元素排序 *
* @param map Map * @param comparator 比较器 */
public static void sort(Map map, Comparator> comparator) {
if (map != null && comparator != null) {
List> list = new ArrayList<>(map.entrySet());
Collections.sort(list, comparator);
map.clear();
for (Map.Entry entry : list) {
put(entry.getKey(), entry.getValue());
}
}
}
/**
* 将集合中的元素转换为指定类型 *
* @param collection 集合 * @param clazz 类型 * @return 转换后的集合 */
public static Collection convert(Collection> collection, Class clazz) {
return collection != null ? new ArrayList<>() {{
for (Object obj : collection) {
add(clazz.cast(obj));
}
}} : null;
}
/**
* 将Map中的元素转换为指定类型 *
* @param map Map * @param clazz 类型 * @return 转换后的Map */
public static Map convert(Map, ?> map, Class clazz) {
return map != null ? new HashMap<>() {{
for (Map.Entry, ?> entry : map.entrySet()) {
put(entry.getKey(), clazz.cast(entry.getValue()));
}
}} : null;
}
/**
* 将集合中的元素转换为指定类型的数组 *
* @param collection 集合 * @param clazz 类型 * @return 转换后的数组 */
public static T[] convertToArray(Collection> collection, Class clazz) {
return collection != null ? (T[]) collection.stream().map(clazz::cast).toArray() : null;
}
/**
* 将Map中的元素转换为指定类型的数组 *
* @param map Map * @param clazz 类型 * @return 转换后的数组 */
public static V[] convertToArray(Map, ?> map, Class clazz) {
return map != null ? (V[]) map.values().stream().map(clazz::cast).toArray() : null;
}
/**
* 将集合中的元素转换为指定类型的List *
* @param collection 集合 * @param clazz 类型 * @return 转换后的List */
public static List convertToList(Collection> collection, Class clazz) {
return collection != null ? new ArrayList<>() {{
for (Object obj : collection) {
add(clazz.cast(obj));
}
}} : null;
}
/**
* 将Map中的元素转换为指定类型的List *
* @param map Map * @param clazz 类型 * @return 转换后的List */
public static List convertToList(Map, ?> map, Class clazz) {
return map != null ? new ArrayList<>() {{
for (Map.Entry, ?> entry : map.entrySet()) {
add(clazz.cast(entry.getValue()));
}
}} : null;
}
/**
* 将集合中的元素转换为指定类型的Set *
* @param collection 集合 * @param clazz 类型 * @return 转换后的Set */
public static Set convertToSet(Collection> collection, Class clazz) {
return collection != null ? new HashSet<>() {{
for (Object obj : collection) {
add(clazz.cast(obj));
}
}} : null;
}
/**
* 将Map中的元素转换为指定类型的Set *
* @param map Map * @param clazz 类型 * @return 转换后的Set */
public static Set convertToSet(Map, ?> map, Class clazz) {
return map != null ? new HashSet<>() {{
for (Map.Entry, ?> entry : map.entrySet()) {
add(clazz.cast(entry.getValue()));
}
}} : null;
}
/**
* 将集合中的元素转换为指定类型的Map *
* @param collection 集合 * @param keyClass 键类型 * @param valueClass 值类型 * @return 转换后的Map */
public static Map convertToMap(Collection> collection, Class keyClass, Class valueClass) {
return collection != null ? new HashMap<>() {{
for (Object obj : collection) {
add(keyClass.cast(obj), valueClass.cast(obj));
}
}} : null;
}
/**
* 将集合中的元素转换为指定类型的Map *
* @param map Map * @param keyClass 键类型 * @param valueClass 值类型 * @return 转换后的Map */
public static Map convertToMap(Map, ?> map, Class keyClass, Class valueClass) {
return map != null ? new HashMap<>() {{
for (Map.Entry, ?> entry : map.entrySet()) {
put(keyClass.cast(entry.getKey()), valueClass.cast(entry.getValue()));
}
}} : null;
}

