I am trying to subscribe observable like :
List<String> colors = Arrays.asList("RED", "BLACK", "WHITE", "GREEN", "YELLOW", "BROWN", "PURPUL", "BLUE");
Observable.just(colors).subscribe(s -> System.out.println(s));
It's working fine but if I use method reference compiler gives error "void is not a functional iterface"
Any one can explain little bit deep? As per me subscriber accept consumer functional interface, which doesn't return anything but we can print stream data like :
Observable.just(colors).subscribe(s -> System.out::println);// NOT COMPILE
Stream is not a functional interface–it has numerous abstract methods.
A functional interface is an interface that contains only one abstract method. They can have only one functionality to exhibit. From Java 8 onwards, lambda expressions can be used to represent the instance of a functional interface. A functional interface can have any number of default methods.
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference. The Runnable interface should be implemented by any class whose instances are intended to be executed by a thread. The class must define a method of no arguments called run .
Runnable and Callable are not examples of functional interface.
Your method reference syntax is wrong. Change it like so,
Observable.just(colors).subscribe(System.out::println);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With