3

I am new in Java 8 and are messing around a little bit.

Now I've tried the andThen method of the functional interface Consumer in Java 8:

public static void main(String[] args) {
    List<Integer> ints = new ArrayList<Integer>();
    for (int i = 0; i < 5; i++) {
        ints.add(i);
    }
    Consumer<Integer> cons1 = in -> System.out.println("--> " + in);
    ints.forEach(cons1.andThen(in -> System.out.println("-+---> " + in)));
}

It works fine! The output is:

--> 0
-+---> 0
--> 1
-+---> 1
--> 2
-+---> 2
--> 3
-+---> 3
--> 4
-+---> 4

Now, I am asking myself if I can concat the both consumers (with the andThen method) without creating an own object for the first consumer cons1?

Tunaki
  • 132,869
  • 46
  • 340
  • 423
mrbela
  • 4,477
  • 9
  • 44
  • 79
  • Wouldn't this be better on the Code Review website, as your code actually works? – AntonH Feb 01 '16 at 20:09
  • You could, but you might have to cast it mid-expression. e.g. `ints.forEach(((Consumer) (n -> System.out.println("--> "+n))).andThen(n -> System.out.println("-+---> "+n)));` – khelwood Feb 01 '16 at 20:10
  • 1
    You can’t concatenate consumers without creating objects for the consumer, as without them, there is nothing to concatenate. But it doesn’t make sense to insist on concatenating anyway, when you don’t have consumer instances in the first place. Just create a single consumer performing both actions… – Holger Feb 02 '16 at 09:40

3 Answers3

8

Yes, but it'll be messier than the straightforward ways of doing it.

You could write

ints.forEach(
   ((Consumer<Integer>) in -> System.out.println("--> " + in))
       .andThen(in -> System.out.println("-+---> " + in)));

but it'd be much better to just write

ints.forEach(in -> {
  System.out.println("--> " + in);
  System.out.println("-+---> " + in);
});
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • 1
    The second one looks much better, but I’m wondering what’s wrong with `in -> System.out.println("--> " + in+"\n-+---> " + in)` or, if it really has to be the system dependent line terminator: `in -> System.out.printf("--> %d%n-+---> %1$d%n", in)`… – Holger Feb 02 '16 at 09:45
2

You could use a cast like in Louis Wasserman's answer.

The "problem" is that a lambda expression can only appear in three different context: assignment context, casting context and invocation context. In your example, you are using an assignment context to create that lambda expression.

It would be nice to be able to write (a -> b).andThen(c -> d) but unfortunately, a lambda expression can't appear in that context... What you can do, is create an invocation context by factoring the consumers into specific methods. You can even make them generic by adding a generic type.

public static void main(String[] args) {
    List<Integer> ints = new ArrayList<Integer>();
    for (int i = 0; i < 5; i++) {
        ints.add(i);
    }
    ints.forEach(printFirst().andThen(printSecond()));
}

private static <T> Consumer<? super T> printFirst() {
    return in -> System.out.println("--> " + in);
}

private static <T> Consumer<? super T> printSecond() {
    return in -> System.out.println("-+---> " + in);
}
Community
  • 1
  • 1
Tunaki
  • 132,869
  • 46
  • 340
  • 423
1

As an addendum to @Tunaki's answer, you could also create a static method that accepts then returns a <T> Consumer<? super T> to simulate a casting context with an invocation context:

public static void main(String... args) {
    List<Integer> ints = new ArrayList<Integer>();
    for (int i = 0; i < 5; i++) {
        ints.add(i);
    }
    ints.forEach(
        consumer(in -> System.out.println("--> " + in))
          .andThen(in -> System.out.println("-+---> " + in))
    );
}

private static <T> Consumer<? super T> consumer(Consumer<? super T> consumer) {
    return consumer;
}
srborlongan
  • 4,460
  • 4
  • 26
  • 33