Interface PeekingIterator<E>

Type Parameters:
E - element type | 元素类型
All Superinterfaces:
Iterator<E>

public interface PeekingIterator<E> extends Iterator<E>
PeekingIterator - Iterator that allows peeking at the next element PeekingIterator - 可查看下一个元素的迭代器

An iterator that supports a one-element lookahead. The peek() method returns the next element without advancing the iterator.

支持单元素前瞻的迭代器。peek() 方法返回下一个元素但不移动迭代器指针。

Features | 主要功能:

  • Peek at next element without consuming - 查看下一个元素但不消费
  • Standard Iterator operations - 标准迭代器操作
  • Useful for parsing and lookahead scenarios - 适用于解析和前瞻场景

Usage Examples | 使用示例:

PeekingIterator<String> it = IteratorUtil.peekingIterator(list.iterator());
while (it.hasNext()) {
    String next = it.peek();  // Look without consuming
    if (shouldProcess(next)) {
        process(it.next());   // Now consume
    } else {
        it.next();            // Skip
    }
}

Security | 安全性:

  • Thread-safe: No - 线程安全: 否
  • Null-safe: Yes (supports null elements) - 空值安全: 是(支持 null 元素)
Since:
JDK 25, opencode-base-collections V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the next element without advancing the iterator 查看下一个元素但不移动指针

    Methods inherited from interface Iterator

    forEachRemaining, hasNext, next, remove
  • Method Details

    • peek

      E peek()
      Returns the next element without advancing the iterator 查看下一个元素但不移动指针

      Multiple calls to peek() return the same element until next() is called.

      多次调用 peek() 返回相同元素,直到调用 next()。

      Returns:
      the next element | 下一个元素
      Throws:
      NoSuchElementException - if no more elements | 如果没有更多元素