The most commonly used tool for Java class libraries summary

The most commonly used tool for Java class libraries are summarized.

Apache Commons

Apache Commons subproject there are many commonly used items are as follows.
Here Insert Picture Description

1、BeanUtils

map and bean interconversion

// bean->map

Map<String, String> map = BeanUtils.describe(user);

// map->bean

BeanUtils.populate(user, map);

We place objects in the cache usually redis in the hash, as follows

设置用户信息

hset student name test

hset student age 10

bean and map the mutual conversion tools is particularly useful in this scenario.

2、Codec

Common coding and decoding methods for encapsulation

// Base64

Base64.encodeBase64String(byte[] binaryData)

Base64.decodeBase64(String base64String)

// MD5

DigestUtils.md5Hex(String data)

// URL

URLCodec.decodeUrl(byte[] bytes);

URLCodec.encodeUrl(BitSet urlsafe, byte[] bytes);

3、Collections

Pay and poor operation

// 判空

CollectionUtils.isEmpty(collA);

// 交集

CollectionUtils.retainAll(collA, collB);

// 并集

CollectionUtils.union(collA, collB);

// 差集

CollectionUtils.subtract(collA, collB);

// 判等

CollectionUtils.isEqualCollection(collA, collB);

4, I / O

Package for IO operations IOUtils

// 拷贝流

IOUtils.copy(InputStream input, OutputStream output);

// 从流中读取内容,转为list

List<String> line = IOUtils.readLines(InputStream input, Charset encoding);

FileUtils package file operation class

File file = new File("/show/data.text");

// 按行读取文件

List<String> lines = FileUtils.readLines(file, "UTF-8");

// 将字符串写入文件

FileUtils.writeStringToFile(file, "test", "UTF-8");

// 文件复制

FileUtils.copyFile(srcFile, destFile);

5, Lang

StringUtils following assertion by testing

// isEmpty的实现 cs == null || cs.length() == 0; return true

assertEquals(true, StringUtils.isEmpty(""));

assertEquals(true, StringUtils.isBlank(null));

assertEquals(true, StringUtils.isBlank(""));

// 空格

assertEquals(true, StringUtils.isBlank(" "));

// 回车

assertEquals(true, StringUtils.isBlank(" "));

Pair and Triple

When two or three want to return values, but no correlation between several values, there is no need for a separate package an object, it can be used as a data structure, or return Pair Triple objects.
Here Insert Picture Description

Google Guava

Create a collection of

// 普通集合的创建

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

Set<String> set = Sets.newHashSet();

// 不可变集合的创建

ImmutableList<String> list = ImmutableList.of("a", "b", "c");

ImmutableSet<String> set = ImmutableSet.of("a", "b");

Immutable set of thread-safe, and can not be changed midway, because add other methods are declared as expired and will throw an exception

public final void add(int index, E element) {

	throw new UnsupportedOperationException();

}

Collection of all kinds of Black &

// use java

Map<String, List<Integer>> map = new HashMap<String, List<Integer>>();

// use guava

Multimap<String, Integer> map = ArrayListMultimap.create();

map.put("key1", 1);

map.put("key1", 2);

System.out.println(map.get("key1"));  // [1, 2]

2 a key mapping value

Table<String, String, Integer> table = HashBasedTable.create();

table.put("a", "a", 1);

table.put("a", "b", 2);

System.out.println(table.get("a", "b"));  // 2

There are many other types of collections, will not be described

stop watch to see the running time of a section of code

Stopwatch stopwatch = Stopwatch.createStarted();

// do something

long second = stopwatch.elapsed(TimeUnit.SECONDS);

TimeUnit can specify the time precision.

Joda Time

Before jdk1.8, only the date operating common class java.util.Date and java.util.Calendar, but the ease of use of these two classes is very bad, SimpleDateFormat is not thread safe. This forced users to choose the date of the operation of third-party class, Joda Time is one of the best. Later, Java itself is aware of this problem, so jdk1.8 borrows heavily from the Joda Time concept, we launched a new date api, LocalDate, LocalTime, LocalDateTime and so on.

Published 61 original articles · won praise 89 · views 20000 +

Guess you like

Origin blog.csdn.net/u014374009/article/details/104626442