import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* List集合工具类
*
*/
public class ListUtils {
/**
* 求2个集合的交集
*
* @param <T>
*
* @param ls
* @param ls2
* @return
*/
@SuppressWarnings("unchecked")
public static <E> List<E> intersect(List<E> ls, List<E> ls2) {
List<E> list = new ArrayList<E>((Collection<? extends E>) Arrays.asList(new Object[ls.size()]));
Collections.copy(list, ls);
list.retainAll(ls2);
return list;
}
/**
* 取2个集合差集
*
* 求ls对ls2的差集,即ls中有,但ls2中没有的
*
* @param ls
* @param ls2
* @return
*/
@SuppressWarnings("unchecked")
public static <E> List<E> diff(List<E> ls, List<E> ls2) {
List<E> list = new ArrayList<E>((Collection<? extends E>) Arrays.asList(new Object[ls.size()]));
Collections.copy(list, ls);
list.removeAll(ls2);
return list;
}
/**
* 求2个集合的并集
*
* @param <E>
*
* @param ls
* @param ls2
* @return
*/
@SuppressWarnings("unchecked")
public static <E> List<E> union(List<E> ls, List<E> ls2) {
List<E> list = new ArrayList<E>((Collection<? extends E>) Arrays.asList(new Object[ls.size()]));
Collections.copy(list, ls);// 将ls的值拷贝一份到list中
list.removeAll(ls2);
list.addAll(ls2);
return list;
}
/**
* 判断对应的List是否为空,true:不为空,false:空
*
* @param list
* @return
*/
public static boolean isNotEmpty(List<?> list) {
if ((null != list) && list.size() > 0) {
return true;
}
return false;
}
/**
* 判断对应的List是否为空,false:不为空,true:空
*
* @param list
* @return
*/
public static boolean isEmpty(List<?> list) {
if ((null == list) || list.size() == 0) {
return true;
}
return false;
}
}