CollectionUtil - List判空及常用操作工具类

Posted wcis

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CollectionUtil - List判空及常用操作工具类相关的知识,希望对你有一定的参考价值。

工具类

/**
 * 集合util
 *
 */
public final class CollectionUtil {

	/**
	 * 返回在curList中第一个在referSet出现的元素
	 * 
	 * @param referSet
	 * @param curList
	 * @return
	 */
	public static <T> T getFirstMatched(Set<T> referSet, List<T> curList) {
		if (isEmpty(referSet) || isEmpty(curList)) {
			return null;
		}
		for (T t : curList) {
			if (referSet.contains(t)) {
				return t;
			}
		}
		return null;
	}

	/**
	 * 返回在curSet中第一个在referSet出现的元素
	 * 
	 * @param referSet
	 * @param curSet
	 * @return
	 */
	public static <T> T getFirstMatched(Set<T> referSet, Set<T> curSet) {
		if (isEmpty(referSet) || isEmpty(curSet)) {
			return null;
		}
		for (T t : curSet) {
			if (referSet.contains(t)) {
				return t;
			}
		}
		return null;
	}

	public static <K, V> List<K> mapKeyToList(Map<K, V> map) {
		if (isEmpty(map)) {
			return Collections.emptyList();
		}
		List<K> list = new ArrayList<K>();
		list.addAll(map.keySet());
		return list;
	}

	public static <K, V> List<V> mapValueToList(Map<K, V> map) {
		if (isEmpty(map)) {
			return Collections.emptyList();
		}
		List<V> list = new ArrayList<V>();
		list.addAll(map.values());
		return list;
	}

	public static <K, V> Set<V> mapValueToSet(Map<K, V> map) {
		if (isEmpty(map)) {
			return Collections.emptySet();
		}
		Set<V> set = new HashSet<V>();
		set.addAll(map.values());
		return set;
	}

	public static <K, V> Set<K> mapKeyToSet(Map<K, V> map) {
		if (isEmpty(map)) {
			return Collections.emptySet();
		}
		return map.keySet();
	}

	public static <K, V> Map<K, V> newHashMap() {
		return new HashMap<K, V>();
	}

	public static <T> Set<T> newHashSet() {
		return new HashSet<T>();
	}

	public static <T> List<T> newArrayList() {
		return new ArrayList<T>();
	}

	public static <T> boolean largeOrEqualSize(List<T> list, int size) {
		return (null == list || 0 > size) ? false : size <= list.size();
	}

	public static <T> boolean largeOrEqualSize(T[] array, int size) {
		return (null == array || 0 > size) ? false : size <= array.length;
	}

	public static <T> boolean largeOrEqualSize(Set<T> set, int size) {
		return (null == set || 0 > size) ? false : size <= set.size();
	}

	public static <K, V> boolean largeOrEqualSize(Map<K, V> map, int size) {
		return (null == map || 0 > size) ? false : size <= map.size();
	}

	public static <T> boolean largeSize(List<T> list, int size) {
		return (null == list || 0 > size) ? false : size < list.size();
	}

	public static <T> boolean largeSize(T[] array, int size) {
		return (null == array || 0 > size) ? false : size < array.length;
	}

	public static <T> boolean largeSize(Set<T> set, int size) {
		return (null == set || 0 > size) ? false : size < set.size();
	}

	public static <K, V> boolean largeSize(Map<K, V> map, int size) {
		return (null == map || 0 > size) ? false : size < map.size();
	}

	public static <T> boolean equalsSize(List<T> list, int size) {
		return (null == list || 0 > size) ? false : size == list.size();
	}

	public static <T> boolean equalsSize(T[] array, int size) {
		return (null == array || 0 > size) ? false : size == array.length;
	}

	public static <T> boolean equalsSize(Set<T> set, int size) {
		return (null == set || 0 > size) ? false : size == set.size();
	}

	public static <K, V> boolean equalsSize(Map<K, V> map, int size) {
		return (null == map || 0 > size) ? false : size == map.size();
	}

	public static <T> boolean isEmpty(T[] array) {
		return null == array || 0 >= array.length;
	}

	public static boolean isEmpty(Collection<?> collection) {
		return null == collection || 0 >= collection.size();
	}

	public static boolean isEmpty(List<?> list) {
		return null == list || 0 >= list.size();
	}

	public static boolean isEmpty(Set<?> set) {
		return null == set || 0 >= set.size();
	}

	public static <T extends Collection<?>> boolean isNotEmpty(T collection) {
		return !isEmpty(collection);
	}

	public static <K, V> boolean isEmpty(Map<K, V> map) {
		return null == map || 0 >= map.size();
	}

	/**
	 * 数组转化为list
	 *
	 * @param array
	 * @return
	 * @throws Exception
	 */
	public static <T> List<T> arrayToList(T[] array) throws Exception {
		List<T> list = new ArrayList<T>();
		if (null == array || 0 >= array.length) {
			return list;
		}
		for (T t : array) {
			list.add(t);
		}
		return list;
	}

	/**
	 * 数组转化为set
	 *
	 * @param array
	 * @return
	 * @throws Exception
	 */
	public static <T> Set<T> arrayToSet(T[] array) {
		Set<T> set = new HashSet<T>();
		if (null == array || 0 >= array.length) {
			return set;
		}
		for (T t : array) {
			set.add(t);
		}
		return set;
	}

	/**
	 * set转化为list
	 *
	 * @param set
	 * @return
	 */
	public static <T> List<T> setToList(Set<T> set) {
		if (null == set || 0 >= set.size()) {
			return Collections.emptyList();
		}
		List<T> list = new ArrayList<T>();
		list.addAll(set);
		return list;
	}

	/**
	 * set转化为list
	 *
	 * @param set
	 * @return
	 */
	public static <T> List<T> toList(Collection<T> set) {
		if (null == set || 0 >= set.size()) {
			return Collections.emptyList();
		}
		List<T> list = new ArrayList<T>();
		list.addAll(set);
		return list;
	}

	/**
	 * set转化为list
	 *
	 * @param list
	 * @return
	 */
	public static <T> Set<T> listToSet(List<T> list) {
		if (null == list || 0 >= list.size()) {
			return Collections.emptySet();
		}
		Set<T> set = new HashSet<T>();
		set.addAll(list);
		return set;
	}

	public static <T> List<T> collectionToList(Collection<T> collection) throws Exception {
		List<T> list = new ArrayList<T>();
		if (null == collection || 0 >= collection.size()) {
			return list;
		}
		for (T t : collection) {
			list.add(t);
		}
		return list;
	}

	/**
	 * orginList中的元素全部加到targetList中,如果其中有null元素 , 则忽略
	 *
	 * @param orginList
	 * @param targetList
	 */
	public static <T> void addAllTrimNull(List<T> orginList, List<T> targetList) {
		if (null == orginList || 0 >= orginList.size() || null == targetList) {
			return;
		}
		for (T t : orginList) {
			if (null != t) {
				targetList.add(t);
			}
		}
	}

	/**
	 * orginList中的元素全部加到targetList中,如果其中有null元素 , 则忽略
	 *
	 * @param orginList
	 * @param targetList
	 */
	public static <T> void copyListNoNull(List<T> orginList, List<T> targetList) {
		if (null == orginList || 0 >= orginList.size() || null == targetList) {
			return;
		}
		for (T t : orginList) {
			if (null != t) {
				targetList.add(t);
			}
		}
	}

	/**
	 * 得到orginList中的所有非空元素的List集合的浅拷贝
	 *
	 * @param orginList
	 * @param orginList
	 */
	public static <T> List<T> getNoNullCopy(List<T> orginList) {
		if (null == orginList || 0 >= orginList.size()) {
			return Collections.emptyList();
		}
		List<T> targetList = new ArrayList<T>();
		for (T t : orginList) {
			if (null != t) {
				targetList.add(t);
			}
		}
		return targetList;
	}

	public static <T> List<T> combine(List<T> firstList, List<T> secondList) {
		List<T> combinedList = new ArrayList<>();
		combinedList.addAll(firstList);
		combinedList.addAll(secondList);
		return combinedList;
	}

	/**
	 * 获取集合第1个元素, 没有元素返回null
	 * 
	 * @param collection
	 * @param <T>
	 * @return
	 */
	public static <T> T first(Collection<T> collection) {
		if (isEmpty(collection)) {
			return null;
		}
		return collection.iterator().next();
	}

	public static void main(String[] args) {
		List<String> list = Lists.newArrayList();

		Set<String> set = Sets.newHashSet();
		if (isEmpty(list)) {
			System.out.println(list);
		}
		if (isEmpty(set)) {
			System.out.println(set);
		}

	}

}

以上是关于CollectionUtil - List判空及常用操作工具类的主要内容,如果未能解决你的问题,请参考以下文章

集合工具类 - CollectionUtil.java

Java基础干货Guava集合工具CollectionUtil源码剖析

数据结构c语言如何实现循环队列

Java中list集合判空

从数据库中获取List判空问题.

map set list 判空并效率比较