Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Sự khác biệt giữa Iterator và ListIterator trong Java

Trong bài viết này, chúng ta sẽ tìm hiểu sự khác biệt giữa Iterator và ListIterator.

Iterator

  • Duyệt được Map, List, Set
  • KHÔNG lấy được index
  • KHÔNG thể modify/replace element
  • Chỉ duyệt forward (tiến)
  • Không thể add element (throw ConcurrentModificationException)
  • Method: next(), remove(), hasNext()

ListIterator

  • Chỉ duyệt List (KHÔNG duyệt Map, Set)
  • Có thể lấy index: nextIndex(), previousIndex()
  • Duyệt CẢ forward VÀ backward (tiến và lùi)
  • Method: nextIndex(), previousIndex(), previous(), next()
  • CÓ THỂ modify/replace element
  • CÓ THỂ add element bất kỳ lúc nào