There are many ready-made toolkits that can speed up development, simplify code writing, and categorize them.
1. String tool class
Use: StringUtils.isNotBlank("ifredom");
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
2. Collection collection tool class (2 types)
- 1. The class
java.util
under the packageCollections
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
Under the packageColletcionUtils
(collections provided by the spring framework are not recommended)
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.collections</artifactId>
<version>3.2.1</version>
</dependency>
use:
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 tool class
Integer integer = new Integer(1);
if (Objects.isNull(integer)) {
System.out.println("对象为空");
}
if (Objects.nonNull(integer)) {
System.out.println("对象不为空");
}
4. Boolean tool class
Like the character tool class, it is recommended to use: commons-lang3
Wrapper class for Boolean: Boolean
, which has three values: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 tools
<!-- IO操作工具类 -->
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.io</artifactId>
<version>2.4</version>
</dependency>
Mainly used in these 3 categories
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. Reflection tools
- 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 tool class
org.springframework.beans.BeanUtils
- Cglib
net.sf.cglib.beans.BeanCopier
use 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());
Use 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. Encryption/Decryption
org.apache.commons.codec.digest.DigestUtils
- md5Hex: MD5 encryption, returns a 32-bit string
- sha1Hex: SHA-1 encryption
- sha256Hex: SHA-256 encryption
- sha512Hex: SHA-512 encryption
- md5: MD5 encryption, returns a 16-bit string
9. Base64 tool class
- java.util.Base64
- org.springframework.util.Base64Utils
10. HttpStatus Http status code
- org.springframework.http.HttpStatus
11. WeChat Pay
<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>
------ If the article is useful to you, thank you in the upper right corner >>> Like | Favorite<<<