You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
3.1 KiB
131 lines
3.1 KiB
package com.qs.cost.common.utils;
|
|
|
|
import java.util.*;
|
|
|
|
/**
|
|
* 常用数据工具类
|
|
*
|
|
* @Author JcYen
|
|
* @Date 2019/5/23
|
|
* @Version 1.0
|
|
*/
|
|
public class CollectionUtil {
|
|
|
|
private CollectionUtil() {
|
|
super();
|
|
}
|
|
|
|
/**
|
|
* 判断一个集合是否为空
|
|
*/
|
|
public static <T> boolean isEmpty(Collection<T> col) {
|
|
if (col == null || col.isEmpty()) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static <T> List<T> setToList(Set<T> colSet){
|
|
if (colSet == null || colSet.isEmpty()){
|
|
return new ArrayList<>();
|
|
}
|
|
List<T> rs = new ArrayList<>(colSet);
|
|
return rs;
|
|
}
|
|
|
|
/**
|
|
* 判断一个集合是否不为空
|
|
*/
|
|
public static <T> boolean isNotEmpty(Collection<T> col) {
|
|
return !isEmpty(col);
|
|
}
|
|
|
|
/**
|
|
* 判断Map是否为空
|
|
*/
|
|
public static <K, V> boolean isEmpty(Map<K, V> map) {
|
|
if (map == null || map.isEmpty()) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 判断Map是否不为空为空
|
|
*/
|
|
public static <K, V> boolean isNotEmpty(Map<K, V> map) {
|
|
return !isEmpty(map);
|
|
}
|
|
|
|
/**
|
|
* 去除list中的重复数据
|
|
*/
|
|
public static <T> List<T> removeRepeat(List<T> list) {
|
|
if (isEmpty(list)) {
|
|
return list;
|
|
}
|
|
List<T> result = new ArrayList<T>();
|
|
for (T e : list) {
|
|
if (!result.contains(e)) {
|
|
result.add(e);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static boolean hasNull(Object... objects){
|
|
for (int i = 0; i < objects.length; i++) {
|
|
if(objects[i]==null){return true;}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static boolean hasNotNull(Object... objects){
|
|
for (int i = 0; i < objects.length; i++) {
|
|
if(objects[i]!=null){return true;}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 将集合转换为String数组
|
|
*/
|
|
public static <T> String[] toArray(List<T> list) {
|
|
if (isEmpty(list)) {
|
|
return null;
|
|
}
|
|
String[] result = new String[list.size()];
|
|
for (int i = 0; i < list.size(); i++) {
|
|
result[i] = String.valueOf(list.get(i));
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* 将list拆分成多给指定的大小的list
|
|
*/
|
|
public static <T> List<List<T>> createList(List<T> target, int size) {
|
|
List<List<T>> listArr = new ArrayList<>();
|
|
//获取被拆分的数组个数
|
|
int arrSize = target.size()%size==0?target.size()/size:target.size()/size+1;
|
|
for(int i=0;i<arrSize;i++) {
|
|
List<T> sub = new ArrayList<T>();
|
|
//把指定索引数据放入到list中
|
|
for(int j=i*size;j<=size*(i+1)-1;j++) {
|
|
if(j<=target.size()-1) {
|
|
sub.add(target.get(j));
|
|
}
|
|
}
|
|
listArr.add(sub);
|
|
}
|
|
return listArr;
|
|
}
|
|
|
|
public static <T> T selectFirst(List<T> list){
|
|
if(isNotEmpty(list)){
|
|
return list.get(0);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
}
|
|
|