1. Java自带工具方法
1.1 List集合拼接成以逗号分隔的字符串
1 | // 如何把list集合拼接成以逗号分隔的字符串 a,b,c |
1.2 比较两个字符串是否相等,忽略大小写
1 | if (strA.equalsIgnoreCase(strB)) { |
1.3 比较两个对象是否相等
当我们用equals比较两个对象是否相等的时候,还需要对左边的对象进行判空,不然可能会报空指针异常,我们可以用java.util包下Objects封装好的比较是否相等的方法
1 | Objects.equals(strA, strB); |
源码是这样的1
2
3public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
1.4 两个List集合取交集
1 | List<String> list1 = new ArrayList<>(); |
2. apache commons工具类库
apache commons是最强大的,也是使用最广泛的工具类库,里面的子库非常多,下面介绍几个最常用的
2.1 commons-lang,java.lang的增强版
建议使用commons-lang3,优化了一些api,原来的commons-lang已停止更新
Maven依赖是:1
2
3
4
5<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
2.1.1 字符串判空
传参CharSequence类型是String、StringBuilder、StringBuffer的父类,都可以直接下面方法判空,以下是源码:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25public static boolean isEmpty(final CharSequence cs) {
return cs == null || cs.length() == 0;
}
public static boolean isNotEmpty(final CharSequence cs) {
return !isEmpty(cs);
}
// 判空的时候,会去除字符串中的空白字符,比如空格、换行、制表符
public static boolean isBlank(final CharSequence cs) {
final int strLen = length(cs);
if (strLen == 0) {
return true;
}
for (int i = 0; i < strLen; i++) {
if (!Character.isWhitespace(cs.charAt(i))) {
return false;
}
}
return true;
}
public static boolean isNotBlank(final CharSequence cs) {
return !isBlank(cs);
}
2.1.2 首字母转成大写
1 | String str = "yideng"; |
2.1.3 重复拼接字符串
1 | String str = StringUtils.repeat("ab", 2); |
2.1.4 格式化日期
1 | // Date类型转String类型 |
2.1.5 包装临时对象
当一个方法需要返回两个及以上字段时,我们一般会封装成一个临时对象返回,现在有了Pair和Triple就不需要了1
2
3
4
5
6/ 返回两个字段
ImmutablePair<Integer, String> pair = ImmutablePair.of(1, "yideng");
System.out.println(pair.getLeft() + "," + pair.getRight()); // 输出 1,yideng
// 返回三个字段
ImmutableTriple<Integer, String, Date> triple = ImmutableTriple.of(1, "yideng", new Date());
System.out.println(triple.getLeft() + "," + triple.getMiddle() + "," + triple.getRight()); // 输出 1,yideng,Wed Apr 07 23:30:00 CST 2021
2.2 commons-collections 集合工具类
Maven依赖是:1
2
3
4
5<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
2.2.1 集合判空
封装了集合判空的方法,以下是源码:1
2
3
4
5
6
7
8
9
10
11
12
13public static boolean isEmpty(final Collection<?> coll) {
return coll == null || coll.isEmpty();
}
public static boolean isNotEmpty(final Collection<?> coll) {
return !isEmpty(coll);
}
// 两个集合取交集
Collection<String> collection = CollectionUtils.retainAll(listA, listB);
// 两个集合取并集
Collection<String> collection = CollectionUtils.union(listA, listB);
// 两个集合取差集
Collection<String> collection = CollectionUtils.subtract(listA, listB);
2.3 common-beanutils 操作对象
Maven依赖:1
2
3
4
5<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>
1 | public class User { |
设置对象属性1
2
3
4
5User user = new User();
BeanUtils.setProperty(user, "id", 1);
BeanUtils.setProperty(user, "name", "yideng");
System.out.println(BeanUtils.getProperty(user, "name")); // 输出 yideng
System.out.println(user); // 输出 {"id":1,"name":"yideng"}
对象和map互转1
2
3
4
5
6
7/ 对象转map
Map<String, String> map = BeanUtils.describe(user);
System.out.println(map); // 输出 {"id":"1","name":"yideng"}
// map转对象
User newUser = new User();
BeanUtils.populate(newUser, map);
System.out.println(newUser); // 输出 {"id":1,"name":"yideng"}
2.4 commons-io 文件流处理
Maven依赖:1
2
3
4
5<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>
文件处理1
2
3
4
5
6
7File file = new File("demo1.txt");
// 读取文件
List<String> lines = FileUtils.readLines(file, Charset.defaultCharset());
// 写入文件
FileUtils.writeLines(new File("demo2.txt"), lines);
// 复制文件
FileUtils.copyFile(srcFile, destFile);
3. Google Guava 工具类库
Maven依赖:1
2
3
4
5<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>
3.1 创建集合
1 | List<String> list = Lists.newArrayList(); |
3.2 黑科技集合
3.2.1 Multimap 一个key可以映射多个value的HashMap
1 | Multimap<String, Integer> map = ArrayListMultimap.create(); |
多省事,多简洁,省得你再创建 Map
3.2.2 BiMap 一种连value也不能重复的HashMap
1 | BiMap<String, String> biMap = HashBiMap.create(); |
这其实是双向映射,在某些场景还是很实用的。
3.2.3 Table 一种有两个key的HashMap
1 | // 一批用户,同时按年龄和性别分组 |
3.2.4 Multiset 一种用来计数的Set
1 | Multiset<String> multiset = HashMultiset.create(); |