Pass Java 8 streaming map function as a parameter

Viewed 11309

I have a string which is comma separated an I want to convert it to an array. However in some cases, I require integer parsing, sometimes double. Is there a way I can pass mapToDouble or mapToInt rather than writing the entire thing again.

return Arrays.stream(test.split(",")).mapToDouble(x -> {
        if (StringUtils.isEmpty(x)) {
            return condition ? -1 : 0;
        }
        return Double.parseDouble(x);
}).toArray();

return Arrays.stream(test.split(",")).mapToInt(x -> {
        if (StringUtils.isEmpty(x)) {
            return condition ? -1 : 0;
        }
        return Integer.parseInt(x);
}).toArray();

Is there a way to make this into a function, where I can have a generic function and make store the appropriate array?

2 Answers
Related