Java Guava | Chars.asList() method with Examples
Last Updated :
01 Feb, 2019
The Chars.asList() method of Guava’s Chars Class accepts a char array as a parameter and returns a list which has the fixed size. The returned list is backed by the char array which is passed as the argument. It means the changes made in the array passed as parameter will be reflected back in the list returned by the method and vice-versa.
Syntax:
public static List<Character> asList(char… backingArray)
Parameter: The method accepts a mandatory parameter backingArray which is a char array that is used to back the list.
Return Value: The method Chars.asList() returns a fixed-size list which is backed by the array passed as the argument to the method. In other words, the method returns the list view of the array.
Below examples illustrate the implementation of above method:
Example 1:
import com.google.common.primitives.Chars;
import java.util.List;
class GFG {
public static void main(String[] args)
{
char arr[] = { 'a' , 'b' , 'c' };
List<Character> myList = Chars.asList(arr);
System.out.println(myList);
}
}
|
Example 2:
import com.google.common.primitives.Chars;
import java.util.List;
class GFG {
public static void main(String[] args)
{
char arr[] = { 'H' , 'E' , 'L' , 'L' , 'O' };
List<Character> myList = Chars.asList(arr);
System.out.println(myList);
}
}
|
Reference: https://google.github.io/guava/releases/18.0/api/docs/com/google/common/primitives/Chars.html#asList(char…)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...