background

The once famous Guava is now being used by fewer and fewer people. Newcomers generally only know the CURD operation of Spring Boot + Mybatis. Of course, there are many reasons for this, and Guava functions and design concepts have been absorbed into JDK over time. The rise of specialized frameworks, such as Optional and diamond operators, has made Guava look a bit weak. But Guava is still very powerful and lightweight, allowing us to do it without having to run another application. So I recommend a few good tools here, interested can go to the official website to see. And this is nice, too.

Recommended tools

Supplementary collection

If you’ve ever developed in Java, you’ve probably used the Map, Set,List, and other collection classes. These Java native collections are useful, but some scenarios are not enough. Most of the time you will have to implement a tool yourself, and it will have some problems. But with Guava, you have other options, and it provides you with quite a few handy collections that are sure to solve some of your old problems.

Multiset

A Set that repeats elements is also an unordered List. What’s so special about it?

  1. It’s easy to count,
  2. You can easily pull out non-repeating elements
  3. The memory footprint is proportional to the number of non-repeating elements. With it, you can get rid of the following repetitive code.
Map<String, Integer> counts = new HashMap<String, Integer>();
for (String word : words) {
    Integer count = counts.get(word);
    if (count == null) {
        counts.put(word, 1);
    } else {
        counts.put(word, count + 1); }}Copy the code

By the way, different implementations are provided depending on your needs

BiMap

A Map that supports bidirectional mapping. You need to maintain two maps for the same functionality, while carefully handling additions, deletions, and changes. Use inverse() to invert. It also provides Hash, immutable, Enum, and Enumhash implementations to choose from.

Table

You can use a list, of course, but you can only query through it. If the number is larger, the time complexity will come up. In this case you might want to use a Map<K1,Map<K2,V>> structure, in which case you can use the Table collection.

Table<Vertex, Vertex, Double> weightedGraph = HashBasedTable.create();
weightedGraph.put(v1, v2, 4);
weightedGraph.put(v1, v3, 20);
weightedGraph.put(v2, v3, 5);

weightedGraph.row(v1); // returns a Map mapping v2 to 4, v3 to 20
weightedGraph.column(v3); // returns a Map mapping v1 to 20, v2 to 5
Copy the code

Buffer Cache

There are many reasons to use Guava Cache, and it’s likely that you need a system that supports automatic expiration, but you don’t want to run a Redis and deal with the serialization and deserialization headaches. Guava Cache advantages:

  1. Lightweight is easy to use and does not require a separate boot
  2. The cleanup mechanism is highly customizable
  3. Support the callback
  4. Serialization and deserialization are not required

When can you not use Guava Cache

  • The amount of data you need to store is large, or can be large, and is likely to cause a lot of memory stress
  • You need to fetch cached data across processes and even machines

Event Bus Event Bus

An annotation-based publish-subscribe event bus. You can register EventBus with Spring, get singletons, and register them in individual containers. Finally, Subscribe with @subscribe. The strength codes are as follows.

// Class is typically registered by the container.
class EventBusChangeRecorder {
    @Subscribe public void recordCustomerChange(ChangeEvent e) { recordChange(e.getChange()); }}// somewhere during initialization
eventBus.register(new EventBusChangeRecorder());
// much later
public void changeCustomer(a) {
    ChangeEvent event = getChangeEvent();
    eventBus.post(event);
}
Copy the code

Mathematical tools

You should also use Guava’s math tools, and if you’ve done a lot of math in Java, you should be impressed by the back-and-forth Double conversion. With Guava Math you can use IntMath.sqrt(int,mode) for easy calculations. And efficiency is said to be high.

String handling

Spring’s native string handling classes are also good to use, but not as good as Guava’s Splitter and Joiner. This is not detailed, to use it to know.

conclusion

At present, there are more and more tool classes. I don’t mean to say that there is nothing that can’t be achieved without Guava, but Guava often brings you some surprises, which are easier to use and more lightweight, so I still recommend you to pay attention to it.

reference

Ifeve.com/google-guav…