集合类工具

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;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值