GUAVA original analysis: Primitive Types module principle analysis (Analysis of Guava Primitive Types Module Principles)

GUAVA original analysis: Primitive Types module principle analysis Guava is a very popular Java library developed by Google, which provides many practical tool categories and data structures, which facilitates the daily work of Java developers.The Primitive Types module is part of the Guava library. It provides some tool categories and methods that operates the original type, making it more convenient and efficient when processing the original type.This article will analyze the principle of the Guava Primitive Types module and provide the corresponding Java code example. 1. Basic concept Before understanding the Guava Primitive Types module, let's take a look at a few basic concepts. 1.1 Original type The original type refers to Byte, SHORT, INT, Long, Float, Double, Boolean and Char's basic data types in Java.When using these original types, we usually need to perform corresponding types of boxing and boxing operations. 1.2 Packing and boxing Boxing refers to converting the original type into the corresponding packaging type, and the unboxing refers to the original type that converts the packaging type to the corresponding.Incuffing and boxing operations usually need to consume additional resources and time. 2. Primitive Types module function introduction Guava's Primitive Types module provides a series of tool categories and methods to handle the original type of boxing and boxing operations, thereby improving performance and simplifying code.Next, we will introduce several main functions of the module. 2.1 Primitives class The core category of the Primitive Types module is the Primitives class, which provides some static methods to judge, transform and operate original types. 2.1.1 Judgment method The Primitives class provides a series of ISXXX () methods to determine whether the given type belongs to the original type.For example, the ISINT () method is used to determine whether the given type is int. boolean isInt = Primitives.isInt(Integer.class); // false 2.1.2 Conversion method The Primitives class also provides a series of WRAP () methods to convert the original type into the corresponding packaging type, and a series of unwrap () methods to convert the packaging type to the corresponding original type.These methods allow us to be loaded and unpacking operations when needed. List<Integer> list = Lists.newArrayList(1, 2, 3); int[] array = Primitives.unwrap(list.toArray(new Integer[0])); 2.1.3 The default value method The Primitives class also provides a series of defaultValue () methods to obtain the default value of the original type.For example, the defaultValue (int.class) will return 0, and the defaultValue (Boolean.class) will return false. int defaultValue = Primitives.defaultValue(int.class); // 0 2.2 Typetoken class Guava's Primitive Types module also provides a Typetoken class that is used to obtain the original type and its corresponding packaging type type token, which further simplifies the type operation. TypeToken<List<Integer>> token = new TypeToken<List<Integer>>() {}; Class<?> rawType = token.getRawType(); // class java.util.List Type[] typeArgs = token.getTypeArgs(); // [class java.lang.Integer] 2.3 Other tools and methods In addition to the Primitives and Typetoken classes, the Primitive Types module of Guava also provides some other tool categories and methods to handle operations such as array, mapping and sorting of original types.These tool types and methods greatly simplify the complexity of the original type operation. Third, sample code Next, we will use some example code to demonstrate the use of the Guava Primitive Types module. import com.google.common.collect.Lists; import com.google.common.primitives.Primitives; import com.google.common.reflect.TypeToken; import java.lang.reflect.Type; import java.util.List; public class PrimitiveTypesExample { public static void main(String[] args) { boolean isInt = Primitives.isInt(Integer.class); // false System.out.println("isInt: " + isInt); List<Integer> list = Lists.newArrayList(1, 2, 3); int[] array = Primitives.unwrap(list.toArray(new Integer[0])); System.out.println("array: " + Arrays.toString(array)); int defaultValue = Primitives.defaultValue(int.class); // 0 System.out.println("defaultValue: " + defaultValue); TypeToken<List<Integer>> token = new TypeToken<List<Integer>>() {}; Class<?> rawType = token.getRawType(); Type[] typeArgs = token.getTypeArgs(); System.out.println("rawType: " + rawType); System.out.println("typeArgs: " + Arrays.toString(typeArgs)); } } In the above code, we first use the ISINT () method of the Primitives class to determine whether the Integer.Class is the original type.Then, we use the UNWRAP () method of the Primitives class to convert the list of Integer type into an array of INT type.Then, we use the defaultValue () method of the Primitives class to obtain the default value of the original type type.Finally, we use the TypeToken class to obtain the original type and generic parameters of the list <Integer> type. Fourth, summary This article analyzes and explains the Primitive Types module of Guava from three aspects: basic concepts, Guava Primitive Types module function introduction and sample code.Through this module, we can handle the original type more convenient and efficiently, simplify code, and improve performance.In the actual Java development, especially when processing a large amount of original type data, it is recommended to use Guava's Primitive Types module to better play its advantages.