Teddu Teddu - 5 months ago 35
Java Question

contains() method is not working for Arrays.asList in java

I have one string which as below:

String color = "black, pink, blue, yellow";

Now I want to convert it into an array and find a color. Something like this:

boolean check = Arrays.asList(color).contains("pink");

Which always gives false.
Can anyone help me with this?


Your string variable color is not an array, so first of all you need to create array from that string variable with split(String dilemeter) method and create ArrayList from splitted string, like this:

List<String> arrList = Arrays.asList(color.split(", "));

After that you can check if arrList contains some element:

boolean check = arrList.contains("pink");