3

I want to split a string by "||" and count how many elements from the resulting array return true for a function boolean isValid(String[] r).

I'm trying to use Arrays.stream to evaluate and filter the array, and finally, filter the resulting array to only keep the true values.

boolean[] truthSet = Arrays.stream(range.split("\\s+))
                           .map(r -> isValid(r))
                           .filter(b -> _whatGoesHere_)
                           .toArray(boolean[]::new);

In place of _whatGoesHere, I tried putting b, b == true, to no avail. What's the right way to achieve this?

4
  • 1
    "to no avail" - why? what is the result? Commented Aug 29, 2019 at 6:28
  • @SharonBenAsher Well, I may be prematurely assuming, but my IDE thinks its a "bad return type in lambda expression. Object cannot be converted to boolean" Commented Aug 29, 2019 at 6:30
  • 3
    The error is not caused by b -> b. You should read it. And post it. It has to do with trying to make an array of boolean (primitive types) from a Stream<java.lang.Boolean>(objects). What's the point in creating an array with only true values anyway? Wouldn't the size of the stream convey the same information? (i.e. there are N valid strings)? Commented Aug 29, 2019 at 6:34
  • 1
    No need to map the elements; you can use filter directly: Arrays.stream(...).filter(str -> isValid(str)).count(). Commented Aug 29, 2019 at 8:42

4 Answers 4

2

You can simply pass itself, the lambda would look like b -> b (because it's already a boolean:

 Boolean[] truthSet = Arrays.stream(range.split("\\s+"))
                       .map(r -> isValid(r))
                       .filter(b -> b)
                       .toArray(Boolean[]::new);

But you're getting only values, that are true and packing them to array - this will produce a boolean array with nothing but true values. Maybe you meant:

 String[] validStrings = Arrays.stream(range.split("\\s+"))
                       .filter(r -> isValid(r))
                       .toArray(String[]::new);

P.S.: To count the values, you can use Stream#count.

Sign up to request clarification or add additional context in comments.

9 Comments

First part doesn't answer this question at all.
.toArray(boolean[]::new); won't work, since the type at that point is Stream<Boolean>. You could use Boolean instead of the primitive type...
that will return an array with all "true" - so what's the difference?
Yeah, I meant to convert it to an array of booleans, but this works too. I Eventually want to count the number of items that returned true for isValid, so this works too. Although, I am curious, if I DID want a boolean array, how would I do it?
@GothamCityRises, to count the number of items you don't need to collect to array, you could use Stream.count() or Collectors.counting() methods. you need to edit the question and state what is the precise requirement
|
0

If you want just count then you can use simple Stream#count

long count = Arrays.stream(range.split("\\s+"))
                       .map(r -> isValid(r))
                       .filter(b -> b)
                       .count();

And Since there no BooleanSteram like IntStream, DoubleStream etc,so you cann't convert Stream<Boolean> to boolean[], in that case, you have to use Boolean[]

Boolean[] truthSet = Arrays.stream(range.split("\\s+"))
                           .map(r -> isValid(r))
                           .filter(b -> b)
                           .toArray(Boolean[]::new); 

2 Comments

filter(b -> b) would compile?
Yes, it will compile and works fine. I believe your isValid(r) method returns a boolean value
0

I hope this is what you want

   Boolean[] truthSet = Arrays.stream(range.split("\\s+"))
            .filter(r -> isValid(r))
            .map(s -> Boolean.TRUE)
            .toArray(Boolean[]::new);

Comments

0

If I read the question correctly the OP asks to split by '||' and is looking for the count, so a (minimal) viable solution would look like this:

import java.util.regex.Pattern;

class Scratch {
    public static void main(String[] args) {
        String test = "||a||a||b||a||bn";

        long count = Pattern.compile("\\|\\|")
                .splitAsStream(test)
                .filter(Scratch::isValid)
                .count();

        System.out.println(count);
    }

    private static boolean isValid(String r) {
        return "a".equals(r);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.