Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I can do the conversion with code like this:

Object[] array = (Object[]) message.get(key);
boolean[] result = new boolean[array.length];
for (int i = 0; i < array.length; i++) {
    result[i] = (boolean) array[i];
}

But, I was think that is possible to do the same conversion using Java 8 streams. I start to code something like this:

boolean[] =  Arrays.stream(array)
                   .map(Boolean.class::cast)
                   .map(Boolean::booleanValue)
                   .toArray()

But this code doesn't work. Compiler says

incompatible types: java.lang.Object[] cannot be converted to boolean[]

I'm trying to understand what is the problem with the code. I think that map(Boolean::booleanValue) would return a stream of boolean values I can collect with toArray.

share|improve this question
2  
This will not work because Object[] and boolean[] are types in Java that are not convertible, so actually Object[] = boolean[] will not work either. – Jagger 2 days ago
2  
.toArray() here is generic, so Object[] is the best you can achieve. You may write your own collector to return boolean[]. – Sasha Salauyou 2 days ago
    
@SashaSalauyou You can however use toArray(IntFunction<A[]> generator) to return a typed array. I added an answer as an example. – George Mulligan 2 days ago
    
There's a related discussion here, for why the primitive streams are limited to only certain types: stackoverflow.com/questions/22435833/… (Why e.g. no CharStream/BooleanStream/etc. like there is IntStream.) – Radiodef 2 days ago

No, because map(Boolean::booleanValue) expands to

map((Boolean value) -> (Boolean)value.booleanValue());

(note the autoboxing inserted by the compiler, because the function passed to map() has to return an Object, not a boolean)

This is why the Java8 streams include a whole bunch of specialized classes (IntStream, LongStream, and DoubleStream). Sadly, there is no specialized stream class for boolean.

share|improve this answer

If you're not opposed to the idea of getting a list instead, you can simply perform collect as your terminal action:

final List<Boolean> boolList = Arrays.stream(array)
                                     .map(Boolean.class::cast)
                                     .map(Boolean::booleanValue)
                                     .collect(Collectors.toList());

The stream as defined here is only capable of producing an Object[] if you attempt to collect it into an array. Using a list will at least allow you to maintain the type you want to convert it into.

share|improve this answer
    
Just out of curiosity, can't the OP convert the list into a boolean array afterwards if they really wanted to? – Jeel Shah 2 days ago
2  
@JeelShah: Yes, but they would be able to get a Boolean[], not a boolean[]. If the OP wanted the boolean[] they'd have to do a bit more work. – Makoto 2 days ago
    
Ahhh okay. Thanks for the additional info! – Jeel Shah 2 days ago

I think it is worth noting too that there is a toArray(IntFunction<A[]> generator) that will at least allow you to have a final result of Boolean[] which is closer to what you wanted.

Boolean[] boolArray =  Arrays.stream(array)
               .map(Boolean.class::cast)
               .map(Boolean::booleanValue)
               .toArray(Boolean[]::new);
share|improve this answer

The only streams of primitive type are IntStream, LongStream and DoubleStream. In particular there is no BooleanStream and there is no idiomatic way to convert a Stream<Boolean> to a boolean[].

If you need this functionality frequently you can use a utility class like this:

public final class BooleanUtils {

    private BooleanUtils() {}

    public static boolean[] listToArray(List<Boolean> list) {
        int length = list.size();
        boolean[] arr = new boolean[length];
        for (int i = 0; i < length; i++)
            arr[i] = list.get(i);
        return arr;
    }

    public static final Collector<Boolean, ?, boolean[]> TO_BOOLEAN_ARRAY
         = Collectors.collectingAndThen(Collectors.toList(), BooleanUtils::listToArray);
} 

Then, given a Stream<Boolean>, you will be able to do:

boolean[] arr = stream.collect(BooleanUtils.TO_BOOLEAN_ARRAY);
share|improve this answer

Java primitive types aren't reference types, so you can't do directly convert Boolean to boolean (autoboxing and unboxing were added to the language in Java 1.5 and don't work with arrays of wrapper types to the primitive types). However, you could use an IntStream.range(int, int) like

boolean[] result = new boolean[array.length];
IntStream.range(0, array.length).forEach(x -> result[x] = (boolean) array[x]);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.