【Java基礎】9、Enumeration介面和Iterator介面的區別,enumerationiterator

來源:互聯網
上載者:User

【Java基礎】9、Enumeration介面和Iterator介面的區別,enumerationiterator

package java.util;


public interface Enumeration<E> {     boolean hasMoreElements();     E nextElement(); } public interface Iterator<E> {     boolean hasNext();     E next();     void remove(); }

 

(01) 函數介面不同

Enumeration 只有2個函數介面。 通過Enumeration,我們只能讀取集合的資料,而不能對資料進行修改。
Iterator 只有3個函數介面。 Iterator除了能讀取集合的資料之外,也能資料進行刪除操作。

(02) Iterator 支援 fail-fast 機制,而 Enumeration 不支援
Enumeration 是JDK 1.0添加的介面。使用到它的函數包括Vector、Hashtable等類,這些類都是JDK 1.0中加入的,Enumeration存在的目的就是為它們提供遍曆介面。Enumeration本身並沒有支援同步,而在Vector、Hashtable實現Enumeration時,添加了同步。
而Iterator 是JDK 1.2才添加的介面,它也是為了HashMap、ArrayList等集合提供遍曆介面。Iterator是支援fail-fast機制的:當多個線程對同一個集合的內容進行操作時,就可能會產生fail-fast事件。

 

iterator是快速失敗的,當你在遍曆的時候,如果另起一個線程來修改它(集合的內容)的結構,這時迭代器會立馬感知到,引起快速失敗,拋出ConcurrentModificationException異常。 所以說iterator 是安全的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.