Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Void is not a functional interface

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
like image 234
Sandeep Tiwari Avatar asked Aug 30 '18 02:08

Sandeep Tiwari


People also ask

What is not a functional interface?

Stream is not a functional interface–it has numerous abstract methods.

What is a functional interface?

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.

Is thread a functional interface?

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 .

What is not true about functional interface?

Runnable and Callable are not examples of functional interface.


1 Answers

Your method reference syntax is wrong. Change it like so,

Observable.just(colors).subscribe(System.out::println);
like image 124
Ravindra Ranwala Avatar answered Oct 22 '22 05:10

Ravindra Ranwala