Streamable接口:empty()方法如何返回Iterable?

Raj*_*hwa 6 java java-8

@FunctionalInterface
public interface Streamable<T> extends Iterable<T>, Supplier<Stream<T>>
Run Code Online (Sandbox Code Playgroud)

我正在探索Streamable接口,遇到的第一个方法是empty()具有以下定义的方法。

static <T> Streamable<T> empty() {
        return Collections::emptyIterator;
    }
Run Code Online (Sandbox Code Playgroud)

Collections::emptyIterator返回,Iterator<T>但是此方法的返回类型为Streamable<T>。Streamble扩展了Iterable和Supplier而不是Iterator接口。

我不知道那怎么运作良好。有人可以帮我理解这一点吗?

我只是在这里没有理解这个概念。我想知道继承是如何工作的,所以我想知道它是如何工作的,但是我无法弄清楚。我想我在这里错过了一些东西。

Era*_*ran 5

empty()返回方法引用Collections::emptyIterator

为了使该代码通过编译,该方法引用必须与Streamable<>接口的单个抽象方法一致。

Collections's emptyIterator() takes no arguments and returns an Iterator<T>.

Streamable<> extends both Iterable<T> and Supplier<Stream<T>>, which means it has to implement two methods (iterator() and get()), but one of them cannot be abstract (otherwise it wouldn't be a functional interface).

Collections's emptyIterator() can conform with Iterable<T>'s Iterator<T> iterator() signature.

So if Streamable<T> has a default implementation of Supplier<Stream<T>>'s get() method (if it doesn't, Streamable<T> cannot be a functional interface), Collections::emptyIterator can implement the Streamable<T> interface.

EDIT: If you were referring to org.springframework.data.util.Streamable, you can see that it does have a default implementation of get():

/*
 * (non-Javadoc)
 * @see java.util.function.Supplier#get()
 */
default Stream<T> get() {
    return stream();
}
Run Code Online (Sandbox Code Playgroud)

Hence, any method reference that conforms with the single abstract method Iterator<T> iterator(), can implement that interface. Therefore Collections::emptyIterator can implement Streamable<T>.