Guava (GOOGLE Common Libraares) original types of common problems in the Java library solution solution
Guava (GOOGLE Common Libraares) original types of common problems in the Java library solution solution
Guava is a powerful Java class library developed by Google, which provides many useful tool categories and functions to facilitate developers for common programming tasks.One of the key areas is the original data type (Primitive Types) of Java, including int, long, double and so on.In this article, we will introduce some common problems about the original type of Guava and give the corresponding solution and Java code examples.
1. How to convert the original data type into an object type?
In Java, the original data type cannot be directly assigned to the object type.Guava provides a basic tool class `com.google.common.primitives` to help us realize this conversion.The following is an example:
import com.google.common.primitives.Ints;
int primitiveInt = 42;
Integer boxedInt = Ints.box(primitiveInt);
In this example, we used the `ints.box ()" method to convert an original int type into a corresponding Integer object.Similar tool classes include `Longs`,` Doubles` and so on.
2. How to convert between the original array and the array?
What should I do if we have an array of the original type, but we need to use an array of the object type?Guava also provides solutions.The following is an example:
import com.google.common.primitives.Ints;
int[] intArray = {1, 2, 3, 4, 5};
Integer[] boxedArray = Ints.asList(intArray).toArray(new Integer[0]);
In this example, we use the `ints.aslist ()` method to convert the original int array into the corresponding list <integer> object, and then use the `Toarray () method to convert List to an object array.
3. How to find the index of specific values in the original type array?
Finding specific values in the original type array is a common problem.GUAVA provides a tool class `com.google.common.primitives` 'to help us realize this function.The following is an example:
import com.google.common.primitives.Ints;
int[] intArray = {1, 2, 3, 4, 5};
int index = Ints.indexOf(intArray, 3);
In this example, we used the `ints.indexof ()` method to find the index of the number 3 in the int array.
4. How to sort the original type array?
In Java, sorting the original type of array requires writing lengthy code.Guava provides a solution that contains the `com.google.Common.primitives` method in` com.google.Common.primitives.The following is an example:
import com.google.common.primitives.Ints;
int[] intArray = {5, 3, 1, 4, 2};
Ints.sort(intArray);
In this example, we use the `ints.sort ()` method to sort the int array.
Summarize:
Guava provides many convenient tool categories and functions to process the original data type of Java.We can use the tool class provided by Guava to convert the original type and object type, perform array operations, find indexes of specific values and sort the array.The above example code can help you understand how to use Guava to deal with common problems related to the original type.
I hope this article will help you understand and use the original Guava original type!