摘要:主要用于遍歷集合中的元素,對象也被稱為迭代器。使用迭代過程中,不可修改集合元素迭代器采用快速失敗機制。一旦迭代過程中檢測到該集合已經(jīng)被修改,程序立即出發(fā)異常,而不是顯示修改后的結(jié)果,避免了共享資源而引發(fā)的潛在問題。
集合類和數(shù)組不一樣,數(shù)組元素既可以是基本類型的值,也可以是對象(實際上保存的是對象的引用變量);而集合里只能保存對象(實際上只是保存對象的引用變量,但通常習慣上認為集合里保存的是對象)。
Collection和Iterator接口
Collection是List,Set,Queue接口的父接口,該接口里定義的方法既可用于操作Set接口,也可用于操作List和Queue集合。
public class CollectionTest { public static void main(String[] args) { Collection使用Lambda表達式遍歷集合collection = new ArrayList<>(); collection.add("1"); collection.add("2"); collection.add("3"); collection.add("rr"); collection.remove("rr"); /*ArrayList arrayList = new ArrayList<>(); arrayList.add("1"); arrayList.add("2"); collection.retainAll(arrayList); System.out.println(collection);//[1, 2] */ //[1, 2, 3] System.out.println(collection); //true System.out.println(collection.contains("1")); ArrayList arrayList = new ArrayList<>(); arrayList.add("1"); arrayList.add("2"); //從Collection中剔除arrayList中的元素 collection.removeAll(arrayList); //[3] System.out.println(collection); //[1, 2] System.out.println(arrayList); //控制Collection集合里只剩下arrayList集合中也包含的元素 arrayList.retainAll(collection); System.out.println(arrayList); //刪除Collection里的元素 collection.clear(); //[] System.out.println(collection); } }
Iterator是Collection接口的父接口。
Lambda表達式遍歷集合元素
public class CollectionEach { public static void main(String[] args) { Collection使用java8增強的Iterator遍歷集合元素。books = new HashSet<>(); books.add("java"); books.add("python"); books.add("Go"); //Consumer函數(shù)接口 Lambda表達式來遍歷集合 books.forEach((e) -> System.out.println(e)); } }
Iterator主要用于遍歷Collection集合中的元素,Iterator對象也被稱為迭代器。
Iterator接口來遍歷集合元素
public class IteratorTest { public static void main(String[] args) { Collectioncollection = new HashSet<>(); collection.add("1"); collection.add("2"); collection.add("3"); Iterator iterator = collection.iterator(); //java8新加的默認方法,該方法可使用Lambda表達式來遍歷集合元素 iterator.forEachRemaining((e) -> { if (e=="1") { iterator.remove(); } System.out.println(e); e="ttt"; }); //[2, 3] System.out.println(collection); // System.out.println(iterator.hasNext()); false /* * iterator 遍歷集合 創(chuàng)建對象后只能使用一次 * */ while (iterator.hasNext()) { String next = iterator.next(); System.out.println(next); } } }
當使用Iterator對集合元素進行迭代時,Iterator并不是把集合元素本身傳給了迭代變量,而是把集合元素的值傳給了迭代變量,所以修改迭代變量的值對集合元素本身并沒有任何影響。
當使用Iterator迭代訪問Collection集合元素時,Collection集合里的元素不能被改變,只有通過Iterator的remove()方法刪除上一次next()方法返回的集合元素才可以,否則引發(fā)異常。
public class IteratorErorTest { public static void main(String[] args) { Collectioncollection = new HashSet<>(); collection.add("1"); collection.add("2"); collection.add("3"); Iterator iterator = collection.iterator(); iterator.forEachRemaining((e) ->{ if (e.equals("1")) { //Exception in thread "main" java.util.ConcurrentModificationException //使用Iterator迭代過程中,不可修改集合元素 collection.remove(e); } }); System.out.println(collection); } }
Iterator迭代器采用快速失敗機制fail-fast。一旦迭代過程中檢測到該集合已經(jīng)被修改,程序立即出發(fā)ConcurrentModificationException異常,而不是顯示修改后的結(jié)果,避免了共享資源而引發(fā)的潛在問題。
使用Lambda表達式遍歷Iteratorpublic class CollectionEach { public static void main(String[] args) { Collection使用forEach循環(huán)遍歷集合元素books = new HashSet<>(); books.add("java"); books.add("python"); books.add("Go"); //Consumer函數(shù)接口 Lambda表達式來遍歷集合 books.forEach((e) -> System.out.println(e)); } }
當使用foreach循環(huán)迭代訪問幾個元素時,該集合也不能被改變。
使用java8新增的Predicate操作集合public class PredicateTest { public static void main(String[] args) { Collectioncollection = new HashSet<>(); collection.add("111"); collection.add("21"); collection.add("13"); //滿足剔除 collection.removeIf((e) -> e.length() ==2); //[111] System.out.println(collection); } }
計數(shù)字符串中a的數(shù)量
public class SumAnum { public static void main(String[] args) { String string = "aavaatgdaaaaaa";//10 System.out.println(sumA(string, (e) ->e.equals("a"))); } private static int sumA(String string,Predicatepredicate) { int nu = 0; for (int i = 0; i < string.length(); i++) { if (string.charAt(i) == "a") { nu++; } } return nu; } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://www.ezyhdfw.cn/yun/69319.html
摘要:集合框架的基本接口類層次結(jié)構(gòu)其中表示接口,表示實現(xiàn)類和在實際開發(fā)中,需要將使用的對象存儲于特定數(shù)據(jù)結(jié)構(gòu)的容器中。實例是迭代器,擁有兩個方法方法迭代器用于遍歷集合元素。返回值則是轉(zhuǎn)換后的數(shù)組,該數(shù)組會保存集合中的所有元素。 Java Collections Framework是Java提供的對集合進行定義,操作,和管理的包含一組接口,類的體系結(jié)構(gòu)。 Java集合框架的基本接口/類層次結(jié)構(gòu)...
摘要:單線程集合本部分將重點介紹非線程安全集合。非線程安全集合框架的最新成員是自起推出的。這是標準的單線程陣營中唯一的有序集合。該功能能有效防止運行時造型。檢查個集合之間不存在共同的元素?;谧匀慌判蚧蛘页黾现械淖畲蠡蜃钚≡?。 【編者按】本文作者為擁有十年金融軟件開發(fā)經(jīng)驗的 Mikhail Vorontsov,文章主要概覽了所有標準 Java 集合類型。文章系國內(nèi) ITOM 管理平臺 O...
摘要:如果需要創(chuàng)建對象,則必須與一個被迭代的集合。這是一個有狀態(tài)的方法該方法用于保證對該流的后續(xù)訪問中最大允許訪問的元素個數(shù)??梢詫显剡M行整體的聚集操作。 Java集合分為Set(無序、不可重復)、List(有序、重復)、Queue(隊列)和Map(映射關系) Java集合概述 數(shù)組元素既可以是基本類型的值,也可以是對象(實際保存對象的引用變量)集合只能保存對象(實際保存對象的引用變量...
集合介紹 本節(jié)介紹Java集合框架,在這里,你將了解集合是什么以及它們?nèi)绾问鼓愕墓ぷ鞲p松、程序更好,你將了解構(gòu)成Java集合框架的核心元素 — 接口、實現(xiàn)、聚合操作和算法。 集合 — 有時稱為容器 — 只是一個將多個元素組合到一個單元中的對象,集合用于存儲、檢索、操作和傳遞聚合數(shù)據(jù)。通常,它們代表形成自然組的數(shù)據(jù)項,例如撲克牌(卡片集合)、郵件文件夾(信件集合)或電話目錄(名稱到電話號碼的映射)...
摘要:第三階段常見對象的學習集合框架概述和集合的遍歷一集合框架的概述集合的由來如果一個程序只包含固定數(shù)量的且其生命周期都是已知的對象,那么這是一個非常簡單的程序。進而它們的遍歷方式也應該是不同的,最終就沒有定義迭代器類。 第三階段 JAVA常見對象的學習 集合框架概述和集合的遍歷 (一) 集合框架的概述 (1) 集合的由來 如果一個程序只包含固定數(shù)量的且其生命周期都是已知的對象,那么這是一...
閱讀 2718·2021-09-09 09:33
閱讀 2886·2019-08-30 15:54
閱讀 2920·2019-08-30 14:21
閱讀 2421·2019-08-29 17:15
閱讀 3635·2019-08-29 16:13
閱讀 2815·2019-08-29 14:21
閱讀 3482·2019-08-26 13:25
閱讀 2080·2019-08-26 12:14