I am kite, the official number “ancient kite”. Articles will be included in JavaNewBee, and there will be a Java back-end knowledge map, which will cover the path from small white to big cow. The public account reply “666” to obtain a large hd picture.

As a wild road developer, UNTIL I came across these tool libraries, I didn’t realize that I had made a lot of repeated wheels, and the wheels didn’t necessarily have the circles of others. I believe that like me, there are many people who make wheels for nothing. Some people are interested in making wheels, and I have nothing to say about this. However, for me, I didn’t know there were wheels in the world before I made them.

For example, when we get a List, we want to nullify the set. I always write it like this:

List<String> list = getList();
if(list ! =null && list.size() > 0) {
    //do something
}
Copy the code

There’s nothing wrong with that, but I’m lazy and tired of typing so much code every time. Some students said, that you wrapped as a method is not on the line, every time to call a method is OK. Here you are, student, building a wheel, someone has written you a series of methods like this.

Let’s get to know these wheels.

Java 8 Stream

Stream is not a library, but it provides a set of methods that allow you to filter, group, transform collections, and more.

For example, the following method implements the function of removing list elements based on a field.

List<User> userList = new ArrayList();
// Add elements
userList =  userList.stream().filter(distinctByKey(user->user.getUserId())).collect(Collectors.toList());

private static <T> Predicate<T> distinctByKey(Function<? superT, ? > keyExtractor) {
 Map<Object,Boolean> seen = new ConcurrentHashMap<>();  return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null; } Copy the code

apache commons

The official address: http://commons.apache.org/

This is not a library, but a series of tool libraries.

Because there are too many libraries, I will not list them, you can go to the official website. Apache Commons Lang and Apache Commons Collections are the most common examples of collection processing, mathematical computation, and IO manipulation.

Apache Commons Lang includes a series of utility classes, including string-dependent, time-processed, reflected, and packaged-out, and so on. Apache Commons Collections are specifically used for collection processing.

Here are a few examples to illustrate, and more details can be found on the official website.

String null operation

String s = "";
Boolean isEmpty = StringUtils.isEmpty(s);
Copy the code

Gets the full name of the class

ClassUtils.getName(ClassUtils.class);
Copy the code

Determines whether the set is empty

Boolean isNotEmpty = CollectionUtils.isNotEmpty(list);
Copy the code

Reflection gets all the fields of a class

Field[] fields = FieldUtils.getAllFields(User.class);
Copy the code

Google Guava

The official address: https://github.com/google/guava

Somewhat like Apache Commons, it contains a series of operation packages such as strings, collections, reflections, mathematical calculations, and so on, and can also be used as a JVM cache.

To give a few examples:

New various objects

List<String> list = Lists.newArrayList();
Set<String> set = Sets.newHashSet();
Map<String,Object> map = Maps.newConcurrentMap();

// Immutable set
ImmutableList<String> immutableList = ImmutableList.of("1"."2"."3"); Copy the code

List to symbol delimited string

List<String> list = new ArrayList<String>();
list.add("1");
list.add("2");
list.add("3");
String result = Joiner.on("-").join(list);
 > 1-2-3 Copy the code

Find intersection, union, difference, etc

For example, the following code finds the intersection of set1 and set2

Set<Integer> set1 = Sets.newHashSet(1.2.3.4.5.6);
Set<Integer> set2 = Sets.newHashSet(1.2.3.4);
       
Sets.SetView<Integer> intersection = Sets.intersection(set1, set2);
Copy the code

Joda Time

The official address: https://www.joda.org/joda-time/

A date, time processing tool library. If you’re not a date handler, you’ll need to query the API almost every time you need it, but with utility classes, it’s just a “. There you have it, and Joda Time is a great library.

Here’s how many days are left until the New Year.

public Days daysToNewYear(LocalDate fromDate) {
  LocalDate newYear = fromDate.plusYears(1).withDayOfYear(1);
  return Days.daysBetween(fromDate, newYear);
}
Copy the code

OkHttp3

The official address: https://square.github.io/okhttp/

An HTTP client that is easy to use and performs well, it is time to abandon HttpClient.

A GET request:

OkHttpClient client = new OkHttpClient();

String run(String url) throws IOException {
  Request request = new Request.Builder()
      .url(url)
 .build();   try (Response response = client.newCall(request).execute()) {  return response.body().string();  } } Copy the code

A POST request:

public static final MediaType JSON
    = MediaType.get("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

String post(String url, String json) throws IOException {  RequestBody body = RequestBody.create(json, JSON);  Request request = new Request.Builder()  .url(url)  .post(body)  .build();  try (Response response = client.newCall(request).execute()) {  return response.body().string();  } } Copy the code

Json series

Jackson

Spring’s default Json serialization tool is sufficient.

Gson

It’s Google, it’s full of features.

FastJson

Ali produced, good algorithm, the best performance.

EasyExcel

The official address: https://www.yuque.com/easyexcel/doc/easyexcel

Ali open source Excel operation tool library, can be seen as Apache POI enhanced packaging version, optimization version.

If you have a large amount of data, you can save memory and improve efficiency with EasyExcel without concurrency risk.

If your Excel is complex enough, using EasyExcel will have a lot less code than if you just used POI.

For example, I implemented the following Excel dynamic export, including dynamic table header, dynamic merge cell function, using very little code, if using POI, it would have more than doubled the code.

excel

TinyPinyin

The official address: https://github.com/promeG/TinyPinyin

Chinese to Pinyin, you input Chinese into pinyin. For example, to achieve such a search function, input “fengzheng” to search, and the word “kite” will be matched, which requires Chinese to be changed to Pinyin.

Some students said, this is not pinyin to English? Of course, it is not changed when entering “fengzheng”, but there is an extra field of pinyin in the record containing “Kite”, so that the search will directly match the pinyin field.

chinese_name pinyin_name
A kite fengzheng

Reflection Tool Library – jOOR

The official address: https://github.com/jOOQ/jOOR

It is a friendly wrapper around the JDK reflection package that implements reflection calls through a series of simple and friendly chain operations. Take this example

public interface StringProxy {
  String substring(int beginIndex);
}

String substring = on("java.lang.String")
 .create("Hello World")  .as(StringProxy.class)  .substring(6); Copy the code

Simple code to achieve JDK dynamic proxy, save a lot of code.

MyBatis-Plus

Official address: https://mp.baomidou.com/

If you have database access in your project, you must have used or at least heard of MyBatis. However, if you only use MyBatis, you will need to write the corresponding SQL Statement for each DAO method (i.e. the code block in mapper.xml). MyBatis provides MyBatis Generator. For example, I have made a Web version of MyBatis Generator. The development efficiency is improved, but each mapper. XML file has a large amount of code. So here comes MyBatis Plus.

The official website defines him as follows:

  1. Just make enhancements without making changes, and introduce it without affecting existing projects, as smooth as silk.
  2. With simple configuration, CRUD operations can be performed quickly, resulting in significant time savings.
  3. Hot loading, code generation, paging, performance analysis, and more.

Finally, the MybatisX IDEA plugin is also available.

vjtools

The official address: https://github.com/vipshop/vjtools

This is the open source toolkit of VipSHOP. Here we mainly introduce the VJKit module, which is the core class library of basic functions such as text, collection and concurrency. I stumbled across this library a long time ago when I was searching for date operations and found the date handling API to be quite comprehensive and useful, which I used in my project for a while.

The last

A good tool library can improve our development efficiency, and it is also a good place to learn source code, as well as other open source frameworks such as Spring and Dubbo, to see how good code is implemented.

If you know any useful, powerful open source toolkit, welcome to share in the comments area, good things can not be exclusive, so that more people benefit.


Strong man wait, first give a praise bar, always white piao, the body can not bear!

I am kite, the official number “ancient kite”. A programmer with both depth and breadth of encouragement teacher, a intended to write poetry but write up the code of rural code farmers! You can follow me now, or you can follow history articles later.