Guava Chars max() and min() method in Java
In this post, we will focus on two of the most useful methods in the Chars class, which are the max() and min() methods. We will see how to use these methods to find the maximum or minimum char value in an array or a varargs parameter. We will also compare them with the native Java approach and show some examples of code snippets.
1. Overview of max() and min() methods
The max() and min() methods are static utility methods that belong to the Chars class in the com.google.common.primitives package. They are used to find the maximum and minimum values in a char array, respectively. The syntax of these methods is as follows:
|
1 2 |
public static char max(char... array) public static char min(char... array) |
The parameter array is a non-empty array of char values. The return value is a char value that is the maximum or minimum value in the array. These methods throw an IllegalArgumentException if the array is empty, and throws NullPointerException if null.
To use these methods, we need to import the com.google.common.primitives.Chars class and pass an array of char values as an argument. For example, if we want to find the maximum and minimum char values in an array of chars, we can write:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import com.google.common.primitives.Chars; class Main { public static void main(String[] args) { char[] arr = {'a', 'b', 'c', 'd', 'e'}; char max = Chars.max(arr); char min = Chars.min(arr); System.out.println("Max: " + max); // e System.out.println("Min: " + min); // a } } |
2. Advantages of max() and min() methods
There are several advantages of using these methods over other approaches, such as:
- They are simple and concise as you don’t need to write any loops or comparisons to find the maximum or minimum value in an array. You just need to pass the array as an argument and get the result.
- They are efficient. They use a single pass algorithm that iterates over the array only once and keeps track of the current maximum or minimum value. They don’t create any intermediate objects or arrays.
- They treat char values strictly numerically, i.e., they are neither Unicode-aware nor locale-dependent. They compare char values based on their numerical order, not their alphabetical order. For example,
Chars.min('A', 'a')will return'A'because it has a lower ASCII code than'a'. - They can accept variable arguments, so you can pass any number of char values as separate arguments.
3. Comparison with native Java approach
The native Java approach for finding the maximum or minimum char value is to loop through the array and call the max() and min() methods for each array element. For example, if we want to find the maximum and minimum char values in an array of chars, we can write:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Main { public static void main(String[] args) { char[] arr = {'a', 'b', 'c', 'd', 'e'}; char min = Character.MAX_VALUE; char max = Character.MIN_VALUE; for (char c : arr) { min = (char)Integer.min(min, c); max = (char)Integer.max(max, c); } System.out.println("Min: " + min); // a System.out.println("Max: " + max); // e } } |
However, using the native Java approach is more verbose and repetitive. We have to use a loop and initialize the max and min variables with the minimum and maximum possible char values. We also have to call the max() and min() methods for each element in the array.
4. Conclusion
In this post, we have learned how to use the max() and min() methods in Guava to find the maximum or minimum char value in an array in Java. We have seen how these methods are more concise, consistent, and convenient than the native Java approach. We have also shown some examples of code snippets that demonstrate the usage and benefits of these methods.
If you are interested in learning more about this method, you can check out the Guava official website or its GitHub repository.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)