Java系列 - 工具类提效

有很多现成的工具包可以加速开发,简化代码书写,将其分门别类归纳.

1. String 工具类

使用: StringUtils.isNotBlank(“ifredom”);

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

2. Collection 集合工具类(2种)

  • 1.java.util包下的 Collections
List<Integer> list = new ArrayList<>();
list.add(2);
list.add(1);
list.add(3);

Collections.sort(list);// 升序
Collections.reverse(list);// 降序

Integer max = Collections.max(list);// 获取最大值
Integer min = Collections.min(list);// 获取最小值

List<Integer> integers = Collections.synchronizedList(list);// 将ArrayList转换成线程安全集合

Collections.emptyList();// 空集合

int i = Collections.binarySearch(list, 3);// 二分查找

List<Integer> unmodifiablIntegers = Collections.unmodifiableList(list); // 转换成不可修改集合

  • 2.org.apache.commons 包下的 ColletcionUtils(不推介使用spring框架提供的collections)
<dependency>
    <groupId>org.apache.directory.studio</groupId>
    <artifactId>org.apache.commons.collections</artifactId>
    <version>3.2.1</version>
</dependency>

使用:

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);

if (CollectionUtils.isEmpty(list)) {
    System.out.println("集合为空");
}

if (CollectionUtils.isNotEmpty(list)) {
    System.out.println("集合不为空");
}

//获取并集
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);

3. Objects 工具类

Integer integer = new Integer(1);

if (Objects.isNull(integer)) {
    System.out.println("对象为空");
}

if (Objects.nonNull(integer)) {
    System.out.println("对象不为空");
}

4. Boolean 工具类

与字符工具类一样,推介使用: commons-lang3

布尔的包装类:Boolean, 它有三种值:null、true、false

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>
Boolean aBoolean = new Boolean(true);
Boolean aBoolean1 = null;

// 判断true或false
System.out.println(BooleanUtils.isTrue(aBoolean));
System.out.println(BooleanUtils.isFalse(aBoolean));

// 判断不为true或不为false
System.out.println(BooleanUtils.isNotTrue(aBoolean));
System.out.println(BooleanUtils.isNotTrue(aBoolean1));

// 转换成数字
System.out.println(BooleanUtils.toInteger(aBoolean));
System.out.println(BooleanUtils.toInteger(aBoolean1));

// Boolean转换成布尔值
System.out.println(BooleanUtils.toBoolean(aBoolean));
System.out.println(BooleanUtils.toBoolean(aBoolean1));
System.out.println(BooleanUtils.toBooleanDefaultIfNull(aBoolean1, false));

5. IO 工具类

<!-- IO操作工具类 -->
<dependency>
    <groupId>org.apache.directory.studio</groupId>
    <artifactId>org.apache.commons.io</artifactId>
    <version>2.4</version>
</dependency>

主要常用到这3个类

  • org.apache.commons.io.IOUtils
  • org.apache.commons.io.FilenameUtils
  • org.apache.commons.io.FileUtils
public void upload(File file) {


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

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

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

	// 读取文件内容到字节数组
	byte[] bytes = IOUtils.toByteArray(new FileInputStream("/temp/a.txt"));



	String originalFilename = file.getOriginalFilename();

	// 获取文件后缀
	String suffix = FilenameUtils.getExtension(originalFilename)
	// 获取返回文件名,不包含后缀
	String filename = FilenameUtils.getBaseName(originalFilename);
}

6. 反射工具类

  • org.springframework.util.ReflectionUtils
@Test
public void reflectionTest() {
	// 获取方法
	Method method = ReflectionUtils.findMethod(User.class, "getId");
	// 获取属性
	Field field = ReflectionUtils.findField(User.class, "id");

	// 执行方法
    User user = new User();			
    user.setId(1L);
    Long id = (Long) ReflectionUtils.invokeMethod(method, user);

	// 判断字段是否常量
	Field field = ReflectionUtils.findField(User.class, "id");
	System.out.println(ReflectionUtils.isPublicStaticFinal(field));

}

7. Bean 工具类

  • org.springframework.beans.BeanUtils
  • Cglib的 net.sf.cglib.beans.BeanCopier

使用BeanUtils

User user1 = new User();
user1.setId(1L);
user1.setName("ifredom");
user1.setAddress("石家庄");

User user2 = new User();

// 拷贝对象
BeanUtils.copyProperties(user1, user2);
System.out.println(user2);

// 实例化某个类
User user = BeanUtils.instantiateClass(User.class);

// 获取指定类的指定方法
Method declaredMethod = BeanUtils.findDeclaredMethod(User.class, "getId");
System.out.println(declaredMethod.getName());

// 获取指定方法的参数
Method declaredMethod = BeanUtils.findDeclaredMethod(User.class, "getId");
PropertyDescriptor propertyForMethod = BeanUtils.findPropertyForMethod(declaredMethod);
System.out.println(propertyForMethod.getName());

使用 BeanCopier

@Test
public void normalCopyTest() {

    final BeanCopier beanCopier = BeanCopier.create(User.class, UserDto.class, false);

    User user = new User();
    user.setAge(18);
    user.setName("ifredom");

    UserDto userDto = new UserDto();

    beanCopier.copy(user, userDto, null);

    Assert.assertEquals(10, userDto.getAge());
    Assert.assertEquals("zhangsan", userDto.getName());
}

8. 加密/解密

org.apache.commons.codec.digest.DigestUtils

  • md5Hex:MD5加密,返回32位字符串
  • sha1Hex:SHA-1加密
  • sha256Hex:SHA-256加密
  • sha512Hex:SHA-512加密
  • md5:MD5加密,返回16位字符串

9. Base64 工具类

  • java.util.Base64
  • org.springframework.util.Base64Utils

10. HttpStatus Http状态码

  • org.springframework.http.HttpStatus

11. 微信支付

 <dependency>
     <groupId>com.github.binarywang</groupId>
     <artifactId>weixin-java-miniapp</artifactId>
     <version>4.3.0</version>
 </dependency>
 <dependency>
     <groupId>com.github.binarywang</groupId>
     <artifactId>weixin-java-common</artifactId>
     <version>4.3.0</version>
 </dependency>
 <dependency>
     <groupId>com.github.binarywang</groupId>
     <artifactId>weixin-java-pay</artifactId>
     <version>4.3.0</version>
 </dependency>

------ 如果文章对你有用,感谢右上角 >>>点赞 | 收藏 <<<

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值