2

I know it can be done by iterating through the elements, but is there a predefined method or a way to use map?

nickt
  • 447
  • 1
  • 4
  • 9
  • 1
    Not 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
  • 1
    What'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
  • 1
    Note 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
  • 2
    And 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 Answers1

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