1

How can I convert Array of Strings into Boolean ArrayList in Java?

For ex: I have String Array like this:

String[] strs= {"true","false","false","true",..etc};

Now, I want all of the above values into a Boolean ArrayList.

List<Boolean> bools=[true, false, false, true, ..etc]

I can do something like this below, but I want this task to be accomplished in one single line of code.

String[] strs={"true","false","false","true",..etc};
List<Boolean> bools=new ArrayList<Boolean>();
for(String x:strs) 
    bools.add(Boolean.parseBoolean(x));
Srikanth Nakka
  • 758
  • 4
  • 16
  • 35

1 Answers1

7

You can use streams with java8

Example:

final String[] stringArray = { "true", "false", "true", etc };
final Boolean[] booleanArray = Arrays.stream(stringArray).map(Boolean::parseBoolean).toArray(Boolean[]::new);
System.out.println(Arrays.toString(booleanArray));
Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97