I know it can be done by iterating through the elements, but is there a predefined method or a way to use map?
Asked
Active
Viewed 564 times
2
-
1Not really (what I know of). I believe you are left to hand-coding it. You may of course try your luck in searching for an external library that can do it. – Ole V.V. Feb 16 '20 at 12:22
-
1What's wrong with https://stackoverflow.com/a/3293970/759042? – aha Feb 16 '20 at 12:27
-
2@aha It doesn’t give an array of *primitive* `boolean`. – Ole V.V. Feb 16 '20 at 12:28
-
1Note quite the same (it is about Integer), but maybe this thread helps: https://stackoverflow.com/questions/718554/how-to-convert-an-arraylist-containing-integers-to-primitive-int-array?noredirect=1&lq=1 – Thilo Feb 16 '20 at 12:29
-
2And then there is also https://stackoverflow.com/q/5615664/14955 – Thilo Feb 16 '20 at 12:30
-
I’d go for the stream solution given at the bottom of [Alex’ answer here](https://stackoverflow.com/a/24720136/5772882). It should work for `boolean` arrays too. – Ole V.V. Feb 16 '20 at 19:21
1 Answers
1
Apache ArrayUtils.toPrimitive
public static boolean[] toPrimitive(Boolean[] array, boolean valueForNull) Converts an array of object Booleans to primitives handling null.
This method returns null for a null input array.
However, this method requires a Boolean array, so first you need to convert your list to an array using toArray
.
ArrayUtils.toPrimitive(list.toArray(ArrayUtils.EMPTY_BOOLEAN_OBJECT_ARRAY));
You will need to download of of the Apache Commons Lang libraries.
https://commons.apache.org/proper/commons-lang/download_lang.cgi

rghome
- 8,529
- 8
- 43
- 62
-
-
-
-
-
1You want to download the Apache commons lang library. It is full of really useful stuff and worth the download. – rghome Feb 16 '20 at 12:34
-
1may not work because my code gets run on an autograder and I doubt they have manually downloaded libraries – nickt Feb 16 '20 at 12:35
-
-
If you can't download a utility library then you may be out of luck, unless there is some trick with streams. – rghome Feb 16 '20 at 12:47