gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 13:11:33 26 4
gpt4 key购买 nike

我有这个代码

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

当我查看 iterator()返回类型,返回 iterator来自 Kotlin.collections 的类型包裹
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{}来自 public interface Iterator<E>java.util;包裹。 IE。
{
/**
* 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()怎么可能可以访问这两个 IteratorKotlin.collections包以及 java.util;包裹?我错过了什么?

最佳答案

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

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

当您同时通过 Kotlin k.c.Iterator<T>k.c.MutableIterator<T>对于 Java 代码,它会看到通常的 Java j.u.Iterator<T> .当你经过 j.u.Iterator<T>到 Kotlin 代码,它看到所谓的 platform type (Mutable)Iterator<T>! .这意味着

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

  • 与标准库中完全独立的集合相比,这种映射背后的动机很简单,例如在 Scala 中完成。在 Java 和 Kotlin 世界之间进行映射时,您不必进行任何复制。缺点是实现的额外复杂性,我们大多不认为是用户。

    Java interoperability Kotlin 文档的部分以获取更多详细信息。

    关于java - list 的迭代器同时实现了 Kotlin.Collection.Iterator 和 Java.util.Iterator?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61916071/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com