Java实用工具库

commons-lang3

1
2
3
4
5
<dependency>  
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency

首字母转成大写

1
StringUtils.capitalize("test");

重复拼接字符串

1
StringUtils.repeat("ab", 2)

格式化日期

Date类型转String类型

1
String date = DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");

String类型转Date类型

1
Date date = DateUtils.parseDate("2021-05-01 01:01:01", "yyyy-MM-dd HH:mm:ss");

一个小时候的日期

1
Date date = DateUtils.addHours(new Date(), 1);

包装临时对象

当一个方法需要返回两个及以上字段时,一般会封装成一个临时对象返回,但Pair和Triple可以完美解决

返回两个字段

1
2
ImmutablePair<Integer, String> pair = ImmutablePair.of(1, "test");
System.out.println(pair.getLeft() + "," + pair.getRight());

返回三个字段

1
2
ImmutableTriple<Integer, String, Date> triple = ImmutableTriple.of(1, "test", new Date());
System.out.println(triple.getLeft() + "," + triple.getMiddle() + "," + triple.getRight());

commons-collections4

1
2
3
4
5
<dependency>  
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>

两个集合取交集

1
Collection<String> collection = CollectionUtils.retainAll(listA, listB);

两个集合取并集

1
Collection<String> collection = CollectionUtils.union(listA, listB);

两个集合取差集

1
Collection<String> collection = CollectionUtils.subtract(listA, listB);

common-beanutils

1
2
3
4
5
<dependency>  
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.4</version>
</dependency>

设置对象属性

1
2
3
4
5
6
7
8
9
10
@Data
public class User {
private Integer id;
private String name;
}

User user = new User();
BeanUtils.setProperty(user, "id", 1);
BeanUtils.setProperty(user, "name", "test");
System.out.println(BeanUtils.getProperty(user, "name"));

对象和map互转

1
2
3
4
Map<String, String> map = BeanUtils.describe(user);

User newUser = new User();
BeanUtils.populate(newUser, map);

commons-io 文件流处理

1
2
3
4
5
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.8.0</version>
</dependency>

读取文件

1
2
File file = new File("demo1.txt");  
List<String> lines = FileUtils.readLines(file, Charset.defaultCharset());

写入文件

1
FileUtils.writeLines(new File("demo2.txt"), lines);

复制文件

1
FileUtils.copyFile(srcFile, destFile);

Guava工具类库

1
2
3
4
5
<dependency>  
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>30.1.1-jre</version>
</dependency>

反转List

1
List<Integer> reverse = Lists.reverse(list);

list集合元素太多,可以分成若干个集合,每个集合10个元素

1
List<List<Integer>> partition = Lists.partition(list, 10);

Multimap一个key可以映射多个value的HashMap

1
2
3
4
5
6
7
8
Multimap<String, Integer> map = ArrayListMultimap.create();  
map.put("key", 1);
map.put("key", 2);
Collection<Integer> values = map.get("key");
// 输出 {"key":[1,2]}
System.out.println(map);
// 还能返回你以前使用的臃肿的Map
Map<String, Collection<Integer>> collectionMap = map.asMap();

BiMap一种连value也不能重复的HashMap

1
2
3
4
5
6
7
8
9
BiMap<String, String> biMap = HashBiMap.create();  
// 如果value重复,put方法会抛异常,除非用forcePut方法
biMap.put("key","value");
// 输出 {"key":"value"}
System.out.println(biMap);
// 既然value不能重复,何不实现个翻转key/value的方法,已经有了
BiMap<String, String> inverse = biMap.inverse();
// 输出 {"value":"key"}
System.out.println(inverse);

Table一种有两个key的HashMap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 一批用户,同时按年龄和性别分组  
Table<Integer, String, String> table = HashBasedTable.create();
table.put(18, "男", "NameA");
table.put(18, "女", "NameB");
// 输出NameA
System.out.println(table.get(18, "男"));
// 这其实是一个二维的Map,可以查看行数据
Map<String, String> row = table.row(18);
// 输出 {"男":"NameA","女":"NameB"}
System.out.println(row);
// 查看列数据
Map<Integer, String> column = table.column("男");
// 输出 {18:"NameA"}
System.out.println(column);

Multiset一种用来计数的Set

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Multiset<String> multiset = HashMultiset.create();  
multiset.add("apple");
multiset.add("apple");
multiset.add("orange");
// 输出 2
System.out.println(multiset.count("apple"));
// 查看去重的元素
Set<String> set = multiset.elementSet();
System.out.println(set); // 输出 ["orange","apple"]
// 还能查看没有去重的元素
Iterator<String> iterator = multiset.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
// 还能手动设置某个元素出现的次数
multiset.setCount("apple", 5);