一、集合相关
1.元素转为集合
Collections.singletonList()
它接收一个元素作为参数,并返回一个包含该元素的单元素列表。这个列表是不可变的,意味着不能对其进行添加、删除或修改操作。
2.判断各种集合是否为空的问题
boolean exist = CollectionUtils.isNotEmpty(dataInfo);
boolean exist1 = CollectionUtils.isEmpty(dataInfo);
3.处理各种集合(Collections)和数组的工具类
import org.apache.commons.collections.CollectionUtils;
CollectionUtils
是 Apache Commons Collections 框架提供的一个工具类,用于操作集合(Collection
)和数组(Array
)。这个工具类包含了各种实用的方法,用于对集合和数组执行各种操作,如查找、过滤、转换等。
- isEmpty/IsNotEmpty: 用于检查集合是否为空或非空。
CollectionUtils.isEmpty(dataList); CollectionUtils.isNotEmpty(dataList);
- addAll: 用于将一个数组或集合中的元素添加到另一个集合中。
CollectionUtils.addAll(collection, elements);
- max/min: 用于查找集合中的最大值和最小值。
CollectionUtils.max(collection, comparator); CollectionUtils.min(collection, comparator);
4.两个list进行复制的自定义工具方法
public static <T> List<T> copyList(List<?> list, Class<T> clazz){
String oldOb = JSON.toJSONString(list);
return JSON.parseArray(oldOb, clazz);
}
二、字符串相关
1.实体转map
BeanUtils.describe() 方法
import org.apache.commons.beanutils.BeanUtils;
Map<String, String>studentMap = BeanUtils.describe(studentEntity);
2.判断字符串是否为空并且是否空字符串
StringUtils.isSpace() true-是空 false-非空
import top.wys.utils.StringUtils;
boolean isString = StringUtils.isSpace(aa);
3.判断字符串是什么类型的进行判断,如是否是数字等
用正则表达式+matches()方法,返回true or false
data.matches("^[+]?\\d*\\.?\\d+$")
// 是否属于数字
常用正则表达式
1、数字:^[0-9]*$
2、n位的数字:^\d{n}$
3、至少n位的数字:^\d{n,}$
4、m-n位的数字:^\d{m,n}$
5、零和非零开头的数字:^(0|[1-9][0-9]*)$
6、非零开头的最多带两位小数的数字:^([1-9][0-9]*)+(\.[0-9]{1,2})?$
7、带1-2位小数的正数或负数:^(\-)?\d+(\.\d{1,2})$
8、正数、负数、和小数:^(\-|\+)?\d+(\.\d+)?$
9、有两位小数的正实数:^[0-9]+(\.[0-9]{2})?$
10、有1~3位小数的正实数:^[0-9]+(\.[0-9]{1,3})?$
11、非零的正整数:^[1-9]\d*$ 或 ^([1-9][0-9]*){1,3}$ 或 ^\+?[1-9][0-9]*$
12、非零的负整数:^\-[1-9][]0-9"*$ 或 ^-[1-9]\d*$
13、非负整数:^\d+$ 或 ^[1-9]\d*|0$
14、非正整数:^-[1-9]\d*|0$ 或 ^((-\d+)|(0+))$