Guava’s Chars Class in Java for primitive char array

Guava com.google.common.primitives package provides utility classes for all primitives types such as Chars, Longs, Ints, Doubles, etc. In this article, we will talk about Guava Chars class which provides static utility methods to work with primitive char array.

 

1. Chars.asList()

Guava’s Chars.asList() method wraps the specified primitive character array as a List<Character> type.

 

Download   Run Code

 

2. Chars.toArray()

Guava’s Chars.toArray() method can be used to convert a List (or Set) of Character to an array of primitive char.

 

Download   Run Code

 

3. Chars.concat()

Guava’s Chars.concat() method combines characters from specified arrays into a single array.

 

Download   Run Code

 

4. Chars.contains()

Guava’s Chars.contains() method determines if the specified character is present in the character array or not.

 

Download   Run Code

 

5. Chars.indexOf() and Chars.lastIndexOf()

Guava’s Chars.indexOf() method returns the index of the first appearance of a given character in array and returns -1 if character is not found in the array.

 

Download   Run Code

 
Guava’s Chars.lastIndexOf() method works in similar way but it returns index of the last appearance instead.

 

6. Chars.join()

Guava’s Chars.join() method returns a string containing characters of the array, separated by separator.

 

Download   Run Code

 

7. Chars.min() and Chars.max()

Guava’s Chars.min() method returns the minimum element present in array and the Chars.max() method returns the maximum element.

 

Download   Run Code

 

 
Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 


Get great deals at Amazon




Leave a Reply

Notify of
avatar
wpDiscuz