Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Guava ImmutableMap Invariant sets in Java

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the example analysis of the Guava ImmutableMap immutable set in Java, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

Source code analysis of Java Guava immutable set ImmutableMap I. case scenario

I've come across a scenario where a large number of put () methods are used to assign values when defining a static-modified Map, something like this--

Public static final Map dayMap= new HashMap (); static {dayMap.put ("Monday", "English class today"); dayMap.put ("Tuesday", "Chinese class today"); dayMap.put ("Wednesday", "math class today"); dayMap.put ("Thursday", "music class today"); dayMap.put ("Sunday", "programming class today");.}

At that time, I was wondering if I could further optimize the code to make the code look more elegant, and then I found that there is a class ImmutableMap in Google Guava, through which chain programming similar to the builder pattern can be implemented. The optimized effect is as follows:

Public static final Map dayMap = ImmutableMap.builder () .put ("Monday", "Today's English lesson") .put ("Tuesday", "Today's Chinese lesson") .put ("Wednesday", "Today's math class") .put ("Thursday", "Today's music lesson") .put ("Sunday", "Today's programming class") .build (); second, ImmutableMap source code analysis

So, how on earth does this ImmutableMap achieve such a function?

In the official tutorial of Google Guava, the collection of Immutable prefixes is defined as immutable sets, including ImmutableSet, ImmutableMap, and so on. What are immutable sets? That is, after the collection is created, all the states in the collection can no longer be modified during the life cycle and can only be read.

So, what can be modified, such as map, list in Jdk, etc., after creation, you can add or modify repeatedly through put () or add (). This is a collection that can be modified again. Since the collection can no longer be modified, is it necessary that it can no longer be modified? No, in fact, it can be modified through reflection, but that's not why immutable sets exist in the first place.

To sum up, immutable collections are thread-safe and can be used as constants.

Next, when you go inside the ImmutableMap, you can see that it implements the Map interface, which is similar to HashMap in that the Map interface is their base class, and the parent class reference points to the subclass object, that is, upward transformation.

Public abstract class ImmutableMap implements Map, Serializable {}

This is an abstract class, and if you want to call ImmutableMap.builder () like this, you can guess on the surface that builder () must be a static method defined by static, go into the source code, and find that this is indeed the case.

/ * Returns a new builder. The generated builder is equivalent to the builder * created by the {@ link Builder} constructor. * / public static Builder builder () {return new Builder ();}

The definition of this method may seem strange to some junior programmers, but the nature of this method format is like this--

Public T method (T t)

This is a generic convention specification, the first to define a generic that indicates that the current method has a generic variable type, represented by T, and the second T to indicate that the return type of method is T.

Looking back at this builder () method, it's easy to understand that it defines a generic type that represents the generic variable of the current method, and Builder represents an object that returns a generic variable.

ImmutableMap.builder () is defined earlier, and in this builder () method, an object of new Builder () is returned, which initializes an array entries of size ImmutableCollection.Builder.DEFAULT_INITIAL_CAPACITY through the constructor, and the default value for this DEFAULT_INITIAL_CAPACITY is 4.

Public static class Builder {Comparator

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report