Guava Chars.contains() method in Java
This post will discuss about Guava Chars.contains() method in Java.
Sometimes you might need to check if a char array contains a particular char value. For example, you might want to search for a character in a word, validate an input, or filter a collection. However, the standard Java arrays class does not provide a built-in method to check if a char array contains a value. You might have to write your own logic, which can be tedious and error-prone.
Fortunately, there is a better way to check if a char array contains a char value in Java, using the Guava Chars class. This class provides static utility methods pertaining to char primitives, that are not already found in either Character or Arrays.
In this post, we will show you how to use the Guava Chars.contains() method, which is one of the methods that you can use to check if a char array contains a char value. We will also explain the benefits and drawbacks of using this method, and compare it with other alternatives.
1. Overview of Chars.contains() method
The Guava Chars.contains() method is a static method that returns true if a char value is present as an element anywhere in the specified array. The syntax of the Chars.contains() method is as follows:
|
1 |
public static boolean contains(char[] array, char target) |
The array parameter is the array of char values in which the target value is to be searched. The target parameter is the char value to be searched for presence in the array. The return value is a boolean indicating whether the target char value is present in the specified array or not.
2. Usage of Chars.contains() method
To use the Chars.contains() method, you need import the com.google.common.primitives.Chars package and call the Chars.contains() method with an array of char values and a target char value as parameters. Then store or use the boolean result returned by the method. For example, suppose you have an array of char values representing a word and you want to check if this array contains the character ‘o’, you can use the following code:
|
1 2 3 4 5 6 7 8 9 |
import com.google.common.primitives.Chars; class Main { public static void main(String[] args) { char[] word = {'h', 'e', 'l', 'l', 'o'}; boolean result = Chars.contains(word, 'o'); System.out.println(result); // true } } |
3. Benefits and drawbacks of using the Chars.contains() method
The Chars.contains() method has some benefits and drawbacks when compared with other methods of checking if a char array contains a char value in Java.
- One benefit is that it simplifies the logic of checking if a char array contains a char value. You don’t have to write any loops or indexes to search for the target value in the array. It is simple to get the result by invoking the method with two arguments.
- Another benefit is that it is consistent and compatible with other methods of the
Charsclass. You can useChars.contains()with any array of char values and any target char value. You can also use it with other methods of theCharsclass, such ascompare(),hashCode(),min(),max(),concat(),indexOf(),lastIndexOf(), etc. - One drawback is that adding Guava as an extra dependency if your project does not already use it can make your project bigger and more complicated.
- Another drawback is that it does not perform any validation or null-checking on the parameters. The
Chars.contains()method does not check if the array isnullor not. This means that you need to ensure that your array is non-null before calling this method. Otherwise, you might get aNullPointerException.
4. Alternatives to Chars.contains() method
If you don’t want to use the Chars.contains() method from Guava, there are some other alternatives that you can use to check if a char array contains a char value in Java. One alternative is to use the standard Arrays.binarySearch() method with a sorted array of char values and a target char value as parameters. This method returns the index of the target value in the sorted array, or a negative value if it is not found. For example, you can use the following code to check if an array of char values contains the character ‘o’:
|
1 2 3 4 5 6 7 8 9 10 |
import java.util.Arrays; class Main { public static void main(String[] args) { char[] word = {'h', 'e', 'l', 'l', 'o'}; Arrays.sort(word); int result = Arrays.binarySearch(word, 'o'); System.out.println(result); // 4 } } |
The result will be 4, which is the index of 'o' in the sorted array. The advantage of using this alternative is that it does not require any external dependency on Guava. It also performs validation and null-checking on the parameters. The disadvantage is that it requires sorting the array before searching for the target value, which can be costly and unnecessary. It also might be less readable and intuitive than using the Chars.contains() method.
5. Conclusion
In this post, we have covered how to use the Guava Chars.contains() method in Java, which is a simple and efficient way to check if a char array contains a char value. We have also explained its benefits and drawbacks, and compared it with some other alternatives that you can use to check if a char array contains a char value in Java.
If you want to learn 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 :)