简体   繁体   中英

Subscriber is not a functional interface

I have the following code. Why is it that, while Flux extends from Publisher, I cannot use a lambda on the subscribe method of the publisher, while I can on the Flux?

        Publisher<String> publisher = new Publisher<String>() {
            @Override
            public void subscribe(Subscriber<? super String> subscriber) {
                subscriber.onNext("Hello world");
                subscriber.onNext("Hello world");
                subscriber.onNext("Hello world");
                subscriber.onNext("Hello world");
                subscriber.onComplete();
            }
        };
        //this lines gives me an error "Subscriber<String> is not a functional interface" 
        publisher.subscribe(System.out::println);

        //no error on this line 
        Flux.just("a","b","c","d")
                .subscribe(System.out::println);

Publisher.subscribe takes a Subscriber as a parameter, which is not a functional interface.

Flux has an overloaded subscribe method which takes a Consumer as a parameter, which is a functional interface. You are in fact using this latter version in your last line.

See Javadoc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM