Chars Class | Guava | Java
Last Updated :
28 May, 2018
Chars is a utility class for primitive type char. It provides Static utility methods pertaining to char primitives, that are not already found in either Character or Arrays. All the operations in this class treat char values strictly numerically, i.e, they are neither Unicode-aware nor locale-dependent.
Declaration :
@GwtCompatible(emulated=true)
public final class Chars
extends Object
Below table shows the Field summary for Guava Chars Class :
Some of the methods provided by Guava Chars Class are :
Exceptions :
- checkedCast : IllegalArgumentException if value is greater than Character.MAX_VALUE or less than Character.MIN_VALUE
- min : IllegalArgumentException if array is empty.
- max : IllegalArgumentException if array is empty.
- fromByteArray : IllegalArgumentException if bytes has fewer than 2 elements.
- ensureCapacity : IllegalArgumentException if minLength or padding is negative.
- toArray : NullPointerException if collection or any of its elements is null.
Below table shows some other methods provided by Guava Chars Class :
Below given are some examples showing the implementation of methods of Guava Chars Class :
Example 1 :
import com.google.common.primitives.Chars;
import java.util.*;
class GFG {
public static void main(String[] args)
{
char arr[] = { 'g' , 'e' , 'e' , 'k' , 's' };
List<Character> myList = Chars.asList(arr);
System.out.println(myList);
}
}
|
Output :
[g, e, e, k, s]
Example 2 :
import com.google.common.primitives.Chars;
import java.util.*;
class GFG {
public static void main(String[] args)
{
List<Character> myList = Arrays.asList( 'g' , 'e' , 'e' , 'k' , 's' );
char [] arr = Chars.toArray(myList);
System.out.println(Arrays.toString(arr));
}
}
|
Output :
[g, e, e, k, s]
Example 3 :
import com.google.common.primitives.Chars;
import java.util.*;
class GFG {
public static void main(String[] args)
{
char [] arr1 = { 'g' , 'e' , 'e' };
char [] arr2 = { 'k' , 's' };
char [] arr = Chars.concat(arr1, arr2);
System.out.println(Arrays.toString(arr));
}
}
|
Output :
[g, e, e, k, s]
Example 4 :
import com.google.common.primitives.Chars;
class GFG {
public static void main(String[] args)
{
char [] arr = { 'g' , 'e' , 'e' , 'k' , 's' };
System.out.println(Chars.contains(arr, 'g' ));
System.out.println(Chars.contains(arr, 'm' ));
}
}
|
output :
true
false
Example 5 :
import com.google.common.primitives.Chars;
class GFG {
public static void main(String[] args)
{
char [] arr = { 'g' , 'e' , 'e' , 'k' , 's' };
System.out.println(Chars.min(arr));
}
}
|
Output :
e
Example 6 :
import com.google.common.primitives.Chars;
class GFG {
public static void main(String[] args)
{
char [] arr = { 'g' , 'e' , 'e' , 'k' , 's' };
System.out.println(Chars.max(arr));
}
}
|
Output :
s
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...