开发常用工具类

工具类

Collections

1.1 排序

@Test
public void test() {
    ArrayList<Integer> list = new ArrayList<>();
    list.add(1);
    list.add(12);
    list.add(3);
    //升序
    Collections.sort(list);
    System.out.println(list);
    //降序
    Collections.reverse(list);
    System.out.println(list);
}

1.2 获取最大值最小值

@Test
public void test1() {
    ArrayList<String> list = new ArrayList<>();
    list.add("1");
    list.add("12");
    list.add("3");
    //最大值
    String max = Collections.max(list);
    //最小值
    String min = Collections.min(list);
}

1.3 返回空集合

Collections.emptyList();

1.4 转换成线程安全的集合

ArrayList、LinkedList、HashMap、HashSet等,都是线程不安全的。可以用Collections的synchronizedxxx方法,将这些线程不安全的集合,直接转换成线程安全集合。

List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);
 
List<Integer> integers = Collections.synchronizedList(list);//将ArrayList转换成线程安全集合
System.out.println(integers);

1.5 转换成不可修改集合

Collections.unmodifiableList(list);

CollectionUtils

org.apache.commons.collections包下的CollectionUtils工具类

2.1 集合判空

CollectionUtils.isEmpty(list)

2.2 对两个集合进行操作

List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);
 
List<Integer> list2 = new ArrayList<>();
list2.add(2);
list2.add(4);
 
//获取并集
Collection<Integer> unionList = CollectionUtils.union(list, list2);
System.out.println(unionList);
 
//获取交集
Collection<Integer> intersectionList = CollectionUtils.intersection(list, list2);
System.out.println(intersectionList);
 
//获取交集的补集
Collection<Integer> disjunctionList = CollectionUtils.disjunction(list, list2);
System.out.println(disjunctionList);
 
//获取差集
Collection<Integer> subtractList = CollectionUtils.subtract(list, list2);
System.out.println(subtractList);

Lists

com.google.guava包下的com.google.common.collect

3.1 创建空集合

List<Integer> list = Lists.newArrayList();

3.2 快速初始化集合

这里初始化出来的集合是可以进行新增和修改操作的。

List<Integer> list = Lists.newArrayList(1, 2, 3);

3.3 笛卡尔积

List<Integer> list1 = Lists.newArrayList(1, 2, 3);
List<Integer> list2 = Lists.newArrayList(4,5);
List<List<Integer>> productList = Lists.cartesianProduct(list1,list2);
System.out.println(productList);

3.4 分页

List<Integer> list = Lists.newArrayList(1, 2, 3, 4, 5);
List<List<Integer>> partitionList = Lists.partition(list, 2);
System.out.println(partitionList);

3.5 顺序转换

List<Integer> list = Lists.newArrayList(3, 1, 2);
List<Integer> reverseList = Lists.reverse(list);
System.out.println(reverseList);
//输出:3,1,3

Objects

4.1 对象判空

@Test
public void test() {
    System.out.println(Objects.isNull(null));
    System.out.println(Objects.nonNull("123"));
}

4.2 对象为空抛异常

@Test
public void test1() {
    Objects.requireNonNull(null);
    Objects.requireNonNull(null, "参数不能为空");
    Objects.requireNonNull(null, () -> "参数不能为空");
}

4.3 判断对象是否相等

此方案可以自动进行判空。

@Test
public void test2() {
    System.out.println(Objects.equals("123", "123"));
    System.out.println(Objects.equals("123", "1234"));
}

BooleanUtils

org.apache.commons.lang.BooleanUtils

5.1 判断布尔类true或false

@Test
public void test() {
    System.out.println(BooleanUtils.toBoolean(new Boolean(true)));
    System.out.println(BooleanUtils.toBoolean(new Boolean(false)));
    System.out.println(BooleanUtils.isFalse(new Boolean(false)));
    System.out.println(BooleanUtils.isTrue(new Boolean(false)));
    System.out.println(BooleanUtils.isNotFalse(new Boolean(false)));
    System.out.println(BooleanUtils.isNotTrue(new Boolean(false)));
}

5.2 转换为数字

将true转为1,false转为0;

@Test
public void test1() {
    System.out.println(BooleanUtils.toInteger(true));
    System.out.println(BooleanUtils.toInteger(false));
}

StringUtils

org.apache.commons.lang3

6.1 字符串判空

@Test
public void test() {
    String str = "  hello world  ";
    System.out.println(StringUtils.isNotBlank(str));
    System.out.println(StringUtils.isBlank(str));
    System.out.println(StringUtils.isNotEmpty(str));
    System.out.println(StringUtils.isEmpty(str));
}

6.2 分割字符串

用String的spilt方法如果字符串为空会报空指针,而StringUtils不会。

StringUtils.split("1,2,3",",");

6.3 判断字符串是否纯数字

这里值得是所有字符都是数组,有小数点也为false。

@Test
public void test2(){
    String str = "12";
    String str1 = "12/";
    String str2 = "1.2";
    System.out.println(StringUtils.isNumeric(str));//true
    System.out.println(StringUtils.isNumeric(str1));//false
    System.out.println(StringUtils.isNumeric(str2));//false
}

6.4 将集合拼接为字符串

List<String> list = Lists.newArrayList("a", "b", "c");
List<Integer> list2 = Lists.newArrayList(1, 2, 3);
System.out.println(StringUtils.join(list, ","));
System.out.println(StringUtils.join(list2, " "));

Assert

org.springframework.util.Assert

若不满足条件则抛出异常,断言

7.1 断言参数是否为空

如果不满足条件,则直接抛异常

@Test
public void test() {
    String str = null;
    Assert.isNull(str, "str必须为空");
    Assert.notNull(str, "str必须为空");
}

7.2 断言集合是否为空

List<String> list = null;
Map<String, String> map = null;
Assert.notEmpty(list, "list不能为空");
Assert.notEmpty(list, () -> "list不能为空");
Assert.notEmpty(map, "map不能为空")

7.3 断言条件是否为空

断言是否满足某个条件,如果不满足条件,则直接抛异常。

List<String> list = null;
Assert.isTrue(CollectionUtils.isNotEmpty(list), "list不能为空");
Assert.isTrue(CollectionUtils.isNotEmpty(list), () -> "list不能为空");

IoUtils

org.apache.commons.io.IoUtils

8.1 读取文件

将某个txt文件中的数据,读取到字符串当中,可以使用IOUtils类的toString方法。

String str = IOUtils.toString(new FileInputStream("/temp/a.txt"), StandardCharsets.UTF_8);
System.out.println(str);

8.2 写入文件

如果你想将某个字符串的内容,写入到指定文件当中,可以使用IOUtils类的write方法

String str = "abcde";
IOUtils.write(str, new FileOutputStream("/temp/b.tx"), StandardCharsets.UTF_8);

8.3 拷贝文件

IOUtils.copy(new FileInputStream("/temp/a.txt"), new FileOutputStream("/temp/b.txt"));

8.4 读取文件内容到字节数组

byte[] bytes = IOUtils.toByteArray(new FileInputStream("/temp/a.txt"));

ClassUtils

org.springframework.util.ClassUtils

10.1 获取对象的所有接口

Class<?>[] allInterfaces = ClassUtils.getAllInterfaces(new User());

10.2 获取某个类的包名

String packageName = ClassUtils.getPackageName(User.class);
System.out.println(packageName);

10.3 判断某个类是否内部类

System.out.println(ClassUtils.isInnerClass(User.class));

10.4 判断对象是否代理对象

System.out.println(ClassUtils.isCglibProxy(new User()));

BeanUtils

org.springframework.beans

11.1 拷贝对象的属性

把某个对象中的所有属性,都拷贝到另外一个对象中

User user1 = new User();
user1.setId(1L);
user1.setName("苏三说技术");
user1.setAddress("成都");
 
User user2 = new User();
BeanUtils.copyProperties(user1, user2);
System.out.println(user2);

11.2 实例化某个类

通过反射实例化一个类的对象,可以使用BeanUtils的instantiateClass方法。

User user = BeanUtils.instantiateClass(User.class);
System.out.println(user);

11.3 获取指定类的指定方法

Method declaredMethod = BeanUtils.findDeclaredMethod(User.class, "getId");
System.out.println(declaredMethod.getName());

11.4 获取指定方法的参数

Method declaredMethod = BeanUtils.findDeclaredMethod(User.class, "getId");
PropertyDescriptor propertyForMethod = BeanUtils.findPropertyForMethod(declaredMethod);
System.out.println(propertyForMethod.getName());

Base64Utils

org.springframework.utilencode和decode方法,用于对数据进行加密和解密。

String str = "abc";
String encode = new String(Base64Utils.encode(str.getBytes()));
System.out.println("加密后:" + encode);
try {
    String decode = new String(Base64Utils.decode(encode.getBytes()), "utf8");
    System.out.println("解密后:" + decode);
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
}

Table(双键Map)

工具包

<dependency>  
    <groupId>com.google.guava</groupId>  
    <artifactId>guava</artifactId>  
    <version>30.1.1-jre</version>  
</dependency>