The most commonly used tool class library in the Java World

Apache Commons

Apache Commons has many subprojects. Common projects are as follows

@Data

public class User {

 private String username;

 private String password;

}

Setting and obtaining properties

User user = new User();

BeanUtils.setProperty(user, "username", "li");

BeanUtils.getProperty(user, "username");

Conversion between map and bean

// bean->map

Map map = BeanUtils.describe(user);

// map->bean

BeanUtils.populate(user, map);

We put the objects in the cache, usually using the hash in redis, as follows

# Set user information

hset student name test

hset student age 10

In this scenario, the tool class for mutual conversion between map and bean is particularly useful

Codec

Common encoding and decoding methods are encapsulated

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

Collections

Operations such as intersection and difference

// Air judgment

CollectionUtils.isEmpty(collA);

// intersection

CollectionUtils.retainAll(collA, collB);

// Union

CollectionUtils.union(collA, collB);

// Difference set

CollectionUtils.subtract(collA, collB);

// Judgment, etc

CollectionUtils.isEqualCollection(collA, collB);

I/O

IOUtils encapsulation of IO operations

// Copy stream

IOUtils.copy(InputStream input, OutputStream output);

// Read the content from the stream and turn it into a list

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

Encapsulation of file operation class by FileUtils

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

// Read file by line

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

// Write string to file

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

// File copy

FileUtils.copyFile(srcFile, destFile);

Lang

StringUtils the following assertion test passed

// Implementation of isEmpty: CS = = null | CS length() == 0;  return true

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

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

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

// Space

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

// enter

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

Pair and Triple

When you want to return 2 or 3 values, but these values have no correlation. It is not necessary to encapsulate an object separately. You can use the following data structure to return Pair or Triple objects

Google Guava

Creation of collections

// Creation of normal collections

List list = Lists.newArrayList();

Set set = Sets.newHashSet();

// Creation of immutable sets

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

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

Immutable collections are thread safe and cannot be changed halfway because methods such as add are declared expired and throw exceptions

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

	throw new UnsupportedOperationException();

}

Various black technology collections

// use java

Map> map = new HashMap>();

// use guava

Multimap map = ArrayListMultimap.create();

map.put("key1", 1);

map.put("key1", 2);

// [1, 2]

System.out.println(map.get("key1"));

Two keys map to one value

Table table = HashBasedTable.create();

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

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

// 2

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

There are many other types of collections that will not be introduced

stop watch

View the running time of a piece of code

Stopwatch stopwatch = Stopwatch.createStarted();

// do something

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

TimeUnit can specify the time precision

Joda Time

jdk1. Before 8, only java was commonly used for date operation classes util. Date and Java util. Calendar, but the ease of use of these two classes is too poor. SimpleDateFormat is not thread safe. This forces users to choose a third-party date operation class, and Joda Time is one of the best. Later, Java itself realized this problem, so jdk1 8. A lot of ideas from Joda Time have been used for reference, and new date APIs have been introduced, such as LocalDate, LocalTime, LocalDateTime, etc. you can see the following articles for usage

https://blog.csdn.net/zzti_erlie/article/details/100849192

The APIs of the two are very similar. If the JDK version of the company is above 1.8, jdk1.0 is recommended 8. For the newly launched date class, if it is below 1.8, Joda Time is recommended

Apache Httpcomponents

Many http tool classes are encapsulated with Apache Httpcomponents. There are many contents. A separate article will

Added by chopperwalker on Mon, 24 Jan 2022 05:51:38 +0200