📜  Java中的迭代器与集合

📅  最后修改于: 2022-05-13 01:54:37.562000             🧑  作者: Mango

Java中的迭代器与集合

IteratorCollection多次帮助和安慰了程序员。但是那里的用法和应用有很大的区别。

1. 迭代器

  • 宣言
public interface Iterator

Type Parameters:
E - the type of elements returned by this iterator
  • Java中的 Collection 框架中使用迭代器来逐个检索元素。
  • 方法总结
Modifier and TypeMethodDescription
default voidforEachRemaining(Consumer action)Performs the given action for each remaining element until all elements have been processed or the action throws an exception.
booleanhasNext()Returns true if the iteration has more elements.
Enext()Returns the next element in the iteration.
default voidremove()Removes from the underlying collection the last element returned by this iterator (optional operation).

2. 收藏

  • 宣言:
public interface Collection extends Iterable

Type Parameters:
E - the type of elements returned by this iterator
  • 集合是一组表示为单个单元的单个对象。 Java提供了集合框架,它定义了几个类和接口来将一组对象表示为一个单元。
  • 方法总结
Modifier and TypeMethodDescription
booleanadd(E e)Ensures that this collection contains the specified element (optional operation).
booleanaddAll(Collection c)Adds all of the elements in the specified collection to this collection (optional operation).
voidclear()Removes all of the elements from this collection (optional operation).
booleancontains(Object o)Returns true if this collection contains the specified element.
booleancontainsAll(Collection c)Returns true if this collection contains all of the elements in the specified collection.
booleanequals(Object o)Compares the specified object with this collection for equality.
inthashCode()Returns the hash code value for this collection.
booleanisEmpty()Returns true if this collection contains no elements.
Iteratoriterator()Returns an iterator over the elements in this collection.
default StreamparallelStream()Returns a possibly parallel Stream with this collection as its source.
booleanremove(Object o)Removes a single instance of the specified element from this collection, if it is present (optional operation).
booleanremoveAll(Collection c)Removes all of this collection’s elements that are also contained in the specified collection (optional operation).
default booleanremoveIf(Predicate filter)Removes all of the elements of this collection that satisfy the given predicate.
booleanretainAll(Collection c)Retains only the elements in this collection that are contained in the specified collection (optional operation).
intsize()Returns the number of elements in this collection.
default Spliteratorspliterator()Creates a Spliterator over the elements in this collection.
default Streamstream()Returns a sequential Stream with this collection as its source.
Object[]toArray()Returns an array containing all of the elements in this collection.
T[]toArray(T[] a)Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.

迭代器对比收藏

  • 迭代器只能移动到next()元素或remove()一个元素。
    但是 Collection 可以add() ,迭代, remove()clear()集合的元素。
  • Iterator 提供了比 Collections 更好的速度,因为 Iterator 接口的操作数量有限。
  • Java.sql.SQLException 扩展了 Iterable 。因此,它允许调用者安全地遍历 SQLException 的原因。
    在这种情况下,使用集合会很昂贵,因为在 n 个异常链中,在 SQLException 接口中使用集合可能需要构造 O(n^2) 个元素。
    然而,使用 Iterable 提供了对异常链的 O(n) 访问。