package com.irededu.modules.iredassignment.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import cn.hutool.core.collection.CollUtil;
/**
* 集合类工具
*
* @author liu
*/
public class CollectUtil extends CollUtil {
private CollectUtil() {
}
/**
* 数组是否包含元素
*
* @param array
* @return
*/
public static boolean isNotEmpty(final Object[] array) {
if (array == null) {
return false;
}
if (array.length == 0) {
return false;
}
return true;
}
/**
* 集合是否等于预想大小
*
* @param collection
* @param size
* @return
*/
public static boolean sizeEqual(final Collection<?> collection, int size) {
if (collection == null) {
return false;
}
return collection.size() == size;
}
/**
* map是否等于预想大小
*
* @param map
* @param size
* @return
*/
public static boolean sizeEqual(final Map<?, ?> map, int size) {
if (map == null) {
return false;
}
return map.size() == size;
}
/**
* 数组是否等于预想大小
*
* @param array
* @param size
* @return
*/
public static boolean sizeEqual(final Object[] array, int size) {
if (array == null) {
return false;
}
return array.length == size;
}
/**
* 去重
*
* @param <T>
* @param collection
* @return
*/
public static <T> List<T> unique(final Collection<T> collection) {
if (collection == null) {
return null;
}
if (collection.isEmpty()) {
return new ArrayList<>();
}
return new ArrayList<>(new LinkedHashSet<>(collection));
}
/**
* 按集合顺序排列映射
*
* @param <K>
* @param <V>
* @param keys
* @param values
* @return
*/
public static <K, V> Map<K, V> mapping(final Collection<K> keys, final Collection<V> values) {
Map<K, V> result = new LinkedHashMap<>();
if (!isNotEmpty(keys) || !isNotEmpty(values)) {
return result;
}
List<V> valueList = new ArrayList<>(values);
int i = 0;
int len = values.size();
Iterator<K> it = keys.iterator();
while (it.hasNext() && i < len) {
K key = it.next();
V value = valueList.get(i);
if (value != null) {
result.put(key, value);
}
i++;
}
return result;
}
/**
* 转string集合
*
* @param objects
* @return
*/
public static List<String> toString(final Collection<?> collection) {
if (collection == null) {
return null;
}
List<String> strList = new ArrayList<>();
for (Object object : collection) {
if (object != null) {
strList.add(object.toString());
} else {
strList.add(null);
}
}
return strList;
}
/**
* 转long集合
*
* @param objects
* @return
*/
public static List<Long> toLong(final Collection<?> collection) {
if (collection == null) {
return null;
}
List<Long> longList = new ArrayList<>();
for (Object object : collection) {
if (object != null) {
longList.add(Long.parseLong(object.toString()));
} else {
longList.add(null);
}
}
return longList;
}
/**
* 转integer集合
*
* @param objects
* @return
*/
public static List<Integer> toInteger(final Collection<?> collection) {
if (collection == null) {
return null;
}
List<Integer> integerList = new ArrayList<>();
for (Object object : collection) {
if (object != null) {
integerList.add(Integer.parseInt(object.toString()));
} else {
integerList.add(null);
}
}
return integerList;
}
/**
* 转string map
*
* @param map
* @return
*/
public static Map<String, String> toString(final Map<?, ?> map) {
if (map == null) {
return null;
}
Map<String, String> strMap = new LinkedHashMap<>();
for (Map.Entry<?, ?> entry : map.entrySet()) {
String keyStr = null;
String valueStr = null;
Object key = entry.getKey();
Object value = entry.getValue();
if (key != null) {
keyStr = key.toString();
}
if (value != null) {
valueStr = value.toString();
}
strMap.put(keyStr, valueStr);
}
return strMap;
}
/**
* 转string为key的map
*
* @param map
* @return
*/
public static <T> Map<String, T> toStringKey(final Map<?, T> map) {
if (map == null) {
return null;
}
Map<String, T> resultMap = new LinkedHashMap<>();
for (Map.Entry<?, T> entry : map.entrySet()) {
String keyStr = null;
Object key = entry.getKey();
if (key != null) {
keyStr = key.toString();
}
resultMap.put(keyStr, entry.getValue());
}
return resultMap;
}
/**
* 转long为key的map
*
* @param map
* @return
*/
public static <T> Map<Long, T> toLongKey(final Map<?, T> map) {
if (map == null) {
return null;
}
Map<Long, T> resultMap = new LinkedHashMap<>();
for (Map.Entry<?, T> entry : map.entrySet()) {
Long keyLong = null;
Object key = entry.getKey();
if (key != null) {
keyLong = Long.parseLong(key.toString());
}
resultMap.put(keyLong, entry.getValue());
}
return resultMap;
}
/**
* 转integer集合
*
* @param objects
* @return
*/
public static <T> Map<Integer, T> toIntegerKey(final Map<?, T> map) {
if (map == null) {
return null;
}
Map<Integer, T> resultMap = new LinkedHashMap<>();
for (Map.Entry<?, T> entry : map.entrySet()) {
Integer keyInteger = null;
Object key = entry.getKey();
if (key != null) {
keyInteger = Integer.parseInt(key.toString());
}
resultMap.put(keyInteger, entry.getValue());
}
return resultMap;
}
}
集合类工具
最新推荐文章于 2024-06-04 23:11:16 发布