JAVA常用类的常用方法,提升开发效率
Collections
- 升序 Collections.sort()
- 降序 Collections.reverse()
- 转换线程安全集合 List integers = Collections.synchronizedList(list);
- 空集合 Collections.emptyList();
- 二分查找 Collections.binarySearch(list, 3);
- 转换成不可修改集合 List integers = Collections.unmodifiableList(list);
org.apache.commons.collections.CollectionUtils
- 判断集合是否为空,CollectionUtils.isEmpty(list)
- 取交集或者并集等。
//获取并集
Collection unionList = CollectionUtils.union(list, list2);
//获取交集
Collection intersectionList = CollectionUtils.intersection(list, list2);
//获取交集的补集
Collection disjunctionList = CollectionUtils.disjunction(list, list2);
//获取差集
Collection subtractList = CollectionUtils.subtract(list, list2);
com.google.common.collect.Lists
- 创建 空集合 List list = Lists.newArrayList();
- 两个集合做笛卡尔积,Lists的cartesianProduct方法可以帮你实现
List<Integer> list1 = Lists.newArrayList(1, 2, 3);
List<Integer> list2 = Lists.newArrayList(4,5);
List<List<Integer>> productList = Lists.cartesianProduct(list1,list2);
结果:
[[1, 4], [1, 5], [2, 4], [2, 5], [3, 4], [3, 5]]
- 将一个大集合分成若干个小集合,可以使用Lists的partition方法:
List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5); List<List<Integer>> partitionList = Lists.partition(list, 2); [[1, 2], [3, 4], [5]]
- 把某个集合转换成另外一个接口,可以使用Lists的 transform方法
List<String> list = Lists.newArrayList("a","b","c"); List<String> transformList = Lists.transform(list, x -> x.toUpperCase());
- 颠倒顺序 Lists.reverse(list)
List<Integer> list = Lists.newArrayList(3, 1, 2);
List<Integer> reverseList = Lists.reverse(list);
结果:
[2, 1, 3]
Objects
- 判断对象是否为空
Objects.isNull(integer) Objects.nonNull(integer)
- 对象为空抛异常
Integer integer1 = new Integer(128); Objects.requireNonNull(integer1); Objects.requireNonNull(integer1, "参数不能为空"); Objects.requireNonNull(integer1, () -> "参数不能为空");
- 判断两个对象是否相等
Integer integer1 = new Integer(1); Integer integer2 = new Integer(1); Objects.equals(integer1, integer2);
BooleanUtils
- 判断true或false
如果你想判断某个参数的值是true或false,可以直接使用isTrue或isFalse方法。例如:
Boolean aBoolean = new Boolean(true); System.out.println(BooleanUtils.isTrue(aBoolean)); System.out.println(BooleanUtils.isFalse(aBoolean))
- 判断不为true或不为false
有时候,需要判断某个参数不为true,即是null或者false。或者判断不为false,即是null或者true。
可以使用isNotTrue或isNotFalse方法。例如:
Boolean aBoolean = new Boolean(true);
Boolean aBoolean1 = null; System.out.println(BooleanUtils.isNotTrue(aBoolean)); System.out.println(BooleanUtils.isNotTrue(aBoolean1)); System.out.println(BooleanUtils.isNotFalse(aBoolean)); System.out.println(BooleanUtils.isNotFalse(aBoolean1));
- 转换成数字
Boolean aBoolean = new Boolean(true);
Boolean aBoolean1 = new Boolean(false); System.out.println(BooleanUtils.toInteger(aBoolean)); --> 1 System.out.println(BooleanUtils.toInteger(aBoolean1)); --> 0
org.apache.commons.io.IOUtils
- 读取文件
如果你想将某个txt文件中的数据,读取到字符串当中,可以使用IOUtils类的toString方法。例如:
String str = IOUtils.toString(new FileInputStream("/temp/a.txt"), StandardCharsets.UTF_8); System.out.println(str); 复制代码
- 写入文件
如果你想将某个字符串的内容,写入到指定文件当中,可以使用IOUtils类的write方法。例如:
String str = "abcde";
IOUtils.write(str, new FileOutputStream("/temp/b.tx"), StandardCharsets.UTF_8); 复制代码
- 文件拷贝
如果你想将某个文件中的所有内容,都拷贝到另一个文件当中,可以使用IOUtils类的copy方法。例如:
IOUtils.copy(new FileInputStream("/temp/a.txt"), new FileOutputStream("/temp/b.txt")); 复制代码
- 读取文件内容到字节数组
如果你想将某个文件中的内容,读取字节数组中,可以使用IOUtils类的toByteArray方法。例如:
byte[] bytes = IOUtils.toByteArray(new FileInputStream("/temp/a.txt"));
org.springframework.util.ReflectionUtils 反射工具类
- 获取方法
如果你想获取某个类的某个方法,可以使用ReflectionUtils类的findMethod方法。例如:
Method method = ReflectionUtils.findMethod(User.class, "getId");
- 获取字段
如果你想获取某个类的某个字段,可以使用ReflectionUtils类的findField方法。例如:
Field field = ReflectionUtils.findField(User.class, "id");
- 执行方法
如果你想通过反射调用某个方法,传递参数,可以使用ReflectionUtils类的invokeMethod方法。例如:
ReflectionUtils.invokeMethod(method, springContextsUtil.getBean(beanName), param);
- 判断字段是否常量
如果你想判断某个字段是否常量,可以使用ReflectionUtils类的isPublicStaticFinal方法。例如:
Field field = ReflectionUtils.findField(User.class, "id"); System.out.println(ReflectionUtils.isPublicStaticFinal(field));
- 判断是否equals方法
如果你想判断某个方法是否equals方法,可以使用ReflectionUtils类的isEqualsMethod方法。例如:
Method method = ReflectionUtils.findMethod(User.class, "getId"); System.out.println(ReflectionUtils.isEqualsMethod(method));