javajava-8functional-programmingjava-stream

How to skip successive elements in a Java stream?


I've been trying to incorporate more functional programming into what I do because of the of the side-effect-free nature of the code I write and its utility in concurrent code. I came across the need to filter out successive elements of a Java stream and couldn't come up with a functional approach better than a plain old imperative approach.

Say I have a program that logs its arguments and I want to filter out two successive elements. For example, -o anOption, -k aSecretKeyWhoseValueWeShouldNotLog, -a anotherOption. What I want in the log is -o anOption, -a anotherOption.

I came up with several approaches, but none of them was as understandable as using a for loop that indexed past the stuff I needed to filter out.

This seems like a fairly common thing to want to do. Is there a pattern, using Java streams or anything else, that is commonly used for this kind of thing?

Here's what I ended up with:

static String filterSecretKeyOutOfCommandLineLogString(final String[] args) {
    return joinArgsToString(filterOptionAndValueOutOfCommandLineLogString(SECRET_KEY, args));
}

private static String joinArgsToString(final String[] args) {
    return Joiner.on(ARG_JOINER_DELIMITER).join(args);
}

private static String[] filterOptionAndValueOutOfCommandLineLogString(final Option option, final String[] args) {
    final List<String> filteredList = new ArrayList<>();

    final int numArgs = args.length;

    for (int i = 0; i < numArgs; ++i) {
        if (Arguments.matchesOption(option, args[i])) {
            ++i;
         } else {
             filteredList.add(args[i]);
         }
     }

     return filteredList.toArray(new String[0]);
}

Solution

  • I'm not sure if this is any better than what you already have. Given that the input is a String[] and the (intermediate) output you want is also a String[] a simple-hearted translation to Java streams could be

    static String[] filterOptionAndValue(String option, String args[]) {
        return IntStream.range(0, args.length)
                .filter(i -> i % 2 == 0)
                .mapToObj(i -> new AbstractMap.SimpleEntry<>(args[i], args[i + 1]))
                .filter(e -> !option.equals(e.getKey()))
                .flatMap(e -> Stream.of(e.getKey(), e.getValue()))
                .toArray(String[]::new);
    }
    
    public static void main(String... env) {
    
        String[] args = {"-o", "opt1", "-k", "secret", "-a", "opt2"};
    
        System.out.println(Arrays.toString(filterOptionAndValue("-k", args)));
    }
    

    [-o, opt1, -a, opt2]