Do springboot, java, springCloud project, these tools commonly used classes, but must remember, and work to reduce some of the workload Oh!

Commons the Apache
the Apache Commons subproject there are many commonly used items are as follows

Action item
Here Insert Picture Description
1, BeanUtils various operations of the Java Bean, copy the object, property
2, Codec processing common coding, decoding
3, Collections Java extended operation set frame
4, I / O input / output means of the package
5, Lang java base object (the java.lang) process tools package
6, BeanUtils
provides a series of operations of the java bean, and the like to read and set property values

@Data
public class User {
    private String username;
    private String password;
}

User user = new User();
BeanUtils.setProperty(user, "username", "li");
BeanUtils.getProperty(user, "username");

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

Setting up user information

hset student name test
hset student age 10

bean and map the mutual conversion tools useful in such a scenario is particularly

Codec
common coding, the decoding method of the package

// 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
deposit operation and poor

// sentenced empty

CollectionUtils.isEmpty(collA);

// intersection

CollectionUtils.retainAll(collA, collB);

// union

CollectionUtils.union(collA, collB);

// set difference

CollectionUtils.subtract(collA, collB);

// equality has

CollectionUtils.isEqualCollection(collA, collB);

The I / O
IOUtils package for IO operations

// copy flow

IOUtils.copy(InputStream input, OutputStream output);

// read from the stream, converted to 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);

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(""));

// Space

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

// Enter

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

Triple Pair and
when two or three want to return values, but no correlation between several values, there is no need for a separate package an object, can be used as a data structure, an object returns Pair or Triple

Pair<Integer, Integer> pair = new ImmutablePair<>(1, 2);
// 1 2
System.out.println(pair.getLeft() + " " + pair.getRight());
Triple<Integer, Integer, Integer> triple = new ImmutableTriple<>(1,2,3);
// 1 2 3
System.out.println(triple.getLeft() + " " + triple.getMiddle() + " " + triple.getRight());

Google Guava
create a collection of

// create a common set of

List<String> list = Lists.newArrayList();
Set<String> set = Sets.newHashSet();

// create a collection of immutable

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);
// [1, 2]
System.out.println(map.get("key1")); 

2 a key mapping value

Table<String, String, Integer> 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, no longer introduce
stop watch
to see the running time of a section of code

Stopwatch stopwatch = Stopwatch.createStarted();
// do something
long second = stopwatch.elapsed(TimeUnit.SECONDS);

Accuracy can specify the time TimeUnit

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, launched a new date API, LocalDate
, LocalTime, the LocalDateTime, etc., you can read the following article to learn about the usage,

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

api 2 are very similar, if the company's jdk version 1.8 is recommended over the use of jdk1.8 new date class, if the following is recommended to use Joda Time 1.8

Apache Httpcomponents
many http tools are packaged with Apache Httpcomponents, more content, follow-up continue to update the list to open an article, you big brother, thank you.

----------------
Disclaimer: This article is the original article CSDN bloggers "tofacebook.com", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and link this statement.

原文链接:http://www.tofacebook.com/post/16374

#springboot #java #springCloud Item # commonly used Tools

Published 37 original articles · won praise 27 · views 6627

Guess you like

Origin blog.csdn.net/cyhlili100/article/details/104036827