Trong bài viết này, chúng ta sẽ tìm hiểu sự khác biệt giữa Iterator và Enumeration interface trong Java.
Iterator
- Là universal cursor (con trỏ phổ dụng)
- Áp dụng cho TẤT CẢ collection classes
- Có method remove()
- KHÔNG phải legacy interface
- Duyệt được: HashMap, LinkedList, ArrayList, HashSet, TreeMap, TreeSet
- Có thể modify collection khi duyệt (remove element)
Enumeration
- KHÔNG phải universal cursor
- Chỉ áp dụng cho legacy classes (Vector, Hashtable)
- KHÔNG có method remove()
- LÀ legacy interface
- Chỉ đọc (read-only) - không modify khi duyệt
- Duyệt được: Vector, Hashtable