Learn GUAVA (Google Common Libraries) Original Type: From entry to proficiency
Learn GUAVA (Google Common Libraries) Original Type: From entry to proficiency
Guava is a set of open source Java libraries developed by Google, which aims to enhance the efficiency and quality of Java programming.Among them, the original Guava original type is an important part of the Guava library. It provides many powerful and easy -to -use tools and classes for operating the original type data.
This article will introduce the basic concepts, common usage and example code of the original type of Guava to help readers from getting started to proficiency.
1. Basic concept
1. Original type conversion: The GUAVA original type library provides many convenient methods to convert the original types, such as converting int to Long, converting Double to Float.
2. Original type tool class: The GUAVA original type library contains a series of tool classes to process common operations of the original type data, such as the conversion, comparison and calculation between the packaging class and the original type.
Second, common usage
1. Original type conversion example:
int intValue = 10;
long longalue = longs.fromint (intvalue); // convert int to long
System.out.println (Longvalue); // Output results: 10
double doubleValue = 3.14;
Float Floatvalue = Floats.tofloat (DoubleValue); // Convert the double to float
System.out.println (floatvalue); // Output results: 3.14
2. Original Type Tool Class Example:
int[] intArray = {1, 2, 3, 4, 5};
int max = ints.max (intron); // Get the maximum value in the INT array
System.out.println (max); // Output results: 5
long[] longArray = {100, 200, 300};
long sum = longs.sum (longarray); // Calculate the sum of all elements in the long array
System.out.println (SUM); // Output results: 600
Third, advanced usage
1. Original type operations: The original type library of the Guava provides some convenient methods to perform the calculation operation of the original type data, such as the addition of methods, subtraction, multiplication, and division.
int a = 10;
int b = 5;
int Sum = ints.Checkedadd (a, B); // Execute the additional calculation and check the overflow
System.out.println (SUM); // Output results: 15
long c = 100;
long d = 20;
long product = longs.saturatedmultiply (c, d); // execute multiplication operation and check overflow
System.out.println (Product); // Output results: 2000
2. Primitive type comparison: The original type library of Guava provides some methods to compare the relationship between the original type data.
int a = 10;
int b = 5;
Int compare = ints.compare (a, b); // Compare the size of the two int values
System.out.println (compare); // Output results: 1 (a> b)
long c = 100;
long d = 200;
long compareunSIGNED = Longs.compareunSigned (c, d); // Compare two long values (unsigned comparison)
System.out.println (compareunsigned); // Output results: -1 (C <D)
Fourth, summary
By learning the original type of Guava, we can easily handle the conversion, comparison and calculation of the original type data.In the actual Java development, using the GUAVA original type library can greatly improve the efficiency and quality of the code.
Please note that this article is only a basic introduction to the original type of Guava. Readers can further learn the official GUAVA official documents to master more advanced functions and usage.
Reference link: https://github.com/google/guava
The above is the content of the original type of Guava, I hope to help you!