Java 8 map streams

maloney :

Is there a way to make this code use Java 8?

public static boolean areBooleansValid(Map<String, Object> pairs, List<String> errors, String... values) {
    for (String value : values) {
        if (pairs.get(value) == null) {
            return false;
        } else if (!(pairs.get(value) instanceof Boolean)) {
            errors.add(value + " does not contain a valid boolean value");
            return false;
        }
    }
    return true;
}

Was thinking something like this:

Stream<Object> e = Stream.of(values).map(pairs::get);

but how can I get it to return the different boolean values from this stream?

curlyBraces :

If you just want to filter out the values that are Boolean and present in the pairs map, you can apply filter function:

Stream.of(values).filter(value ->  pairs.get(value) != null && pairs.get(value) instanceof Boolean)

Or if you want to actually return true and false values, you can use map:

return Stream.of(values).allMatch(value -> {
            if (pairs.get(value) == null) {
                return false;
            }
            if ((pairs.get(value) instanceof Boolean)) {
                return true;
            }
            errors.add(value + " does not contain a valid boolean value");
            return false;
        });

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=87131&siteId=1