list的迭代器同时实现了Kotlin.Collection.Iterator和Java.util.Iterator?



我有这个代码

val list = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9)
list.iterator().forEachRemaining{}

当我检查iterator()返回类型时,它从包中返回Kotlin.collections类型的iterator

public interface Iterator<out T> {
/**
* Returns the next element in the iteration.
*/
public operator fun next(): T
/**
* Returns `true` if the iteration has more elements.
*/
public operator fun hasNext(): Boolean
}

从上面看,没有forEachRemaining{}功能。但是,我仍然可以使用forEachRemaining{}它来自java.util;public interface Iterator<E>。即

{
/**
* Returns {@code true} if the iteration has more elements.
* (In other words, returns {@code true} if {@link #next} would
* return an element rather than throwing an exception.)
*
* @return {@code true} if the iteration has more elements
*/
boolean hasNext();
/**
* Returns the next element in the iteration.
*
* @return the next element in the iteration
* @throws NoSuchElementException if the iteration has no more elements
*/
E next();
/**
* Removes from the underlying collection the last element returned
* by this iterator (optional operation).  This method can be called
* only once per call to {@link #next}.  The behavior of an iterator
* is unspecified if the underlying collection is modified while the
* iteration is in progress in any way other than by calling this
* method.
*
* @implSpec
* The default implementation throws an instance of
* {@link UnsupportedOperationException} and performs no other action.
*
* @throws UnsupportedOperationException if the {@code remove}
*         operation is not supported by this iterator
*
* @throws IllegalStateException if the {@code next} method has not
*         yet been called, or the {@code remove} method has already
*         been called after the last call to the {@code next}
*         method
*/
default void remove() {
throw new UnsupportedOperationException("remove");
}
/**
* Performs the given action for each remaining element until all elements
* have been processed or the action throws an exception.  Actions are
* performed in the order of iteration, if that order is specified.
* Exceptions thrown by the action are relayed to the caller.
*
* @implSpec
* <p>The default implementation behaves as if:
* <pre>{@code
*     while (hasNext())
*         action.accept(next());
* }</pre>
*
* @param action The action to be performed for each element
* @throws NullPointerException if the specified action is null
* @since 1.8
*/
default void forEachRemaining(Consumer<? super E> action) {
Objects.requireNonNull(action);
while (hasNext())
action.accept(next());
}
}

iterator()如何访问Kotlin.collections包和java.util;包的Iterator?我错过了什么吗?

Kotlin 标准库中的某些类会自动映射到特定于平台的类(例如,Kotlin/JVM 的 Java 类(。你提到的Iterator就是这种情况。

请注意,与集合相关的类没有一对一映射。Kotlin 的kotlin.collection.Iterator只包含您在问题中提到的只读操作。它具有同级接口kotlin.collection.MutableIterator,可扩展Iterator并添加remove()方法。这两者都映射到Java的java.util.Iterator中。所以所有的 Kotlin 代码,包括像forEachRemaining这样的扩展方法,都是使用 Kotlin 类型声明的,但 Java 代码将在后台使用。

当您将 Kotlink.c.Iterator<T>k.c.MutableIterator<T>传递给 Java 代码时,它会看到通常的 Javaj.u.Iterator<T>。当您将j.u.Iterator<T>传递给 Kotlin 代码时,它会看到所谓的平台类型(Mutable)Iterator<T>!。这意味着

  1. 您可以根据传递代码 Javadoc 或用法自由声明它为空和非空,因此!类型名称。
  2. 您可以将其用作MutableIteratorIterator,具体取决于您的用例。

这种映射背后的动机很简单,而不是像在 Scala 中那样在标准库中完全独立的集合。在 Java 和 Kotlin 世界之间映射时,您不必执行任何复制。缺点是实现的额外复杂性,我们大多不将其视为用户。

有关更多详细信息,请参阅 Kotlin 文档的 Java 互操作性部分。

相关内容

最新更新