摘要:主要用于遍歷集合中的元素,對(duì)象也被稱為迭代器。使用迭代過(guò)程中,不可修改集合元素迭代器采用快速失敗機(jī)制。一旦迭代過(guò)程中檢測(cè)到該集合已經(jīng)被修改,程序立即出發(fā)異常,而不是顯示修改后的結(jié)果,避免了共享資源而引發(fā)的潛在問(wèn)題。
集合類(lèi)和數(shù)組不一樣,數(shù)組元素既可以是基本類(lèi)型的值,也可以是對(duì)象(實(shí)際上保存的是對(duì)象的引用變量);而集合里只能保存對(duì)象(實(shí)際上只是保存對(duì)象的引用變量,但通常習(xí)慣上認(rèn)為集合里保存的是對(duì)象)。
Collection和Iterator接口
Collection是List,Set,Queue接口的父接口,該接口里定義的方法既可用于操作Set接口,也可用于操作List和Queue集合。
public class CollectionTest { public static void main(String[] args) { Collection使用Lambda表達(dá)式遍歷集合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表達(dá)式遍歷集合元素
public class CollectionEach { public static void main(String[] args) { Collection使用java8增強(qiáng)的Iterator遍歷集合元素。books = new HashSet<>(); books.add("java"); books.add("python"); books.add("Go"); //Consumer函數(shù)接口 Lambda表達(dá)式來(lái)遍歷集合 books.forEach((e) -> System.out.println(e)); } }
Iterator主要用于遍歷Collection集合中的元素,Iterator對(duì)象也被稱為迭代器。
Iterator接口來(lái)遍歷集合元素
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新加的默認(rèn)方法,該方法可使用Lambda表達(dá)式來(lái)遍歷集合元素 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)建對(duì)象后只能使用一次 * */ while (iterator.hasNext()) { String next = iterator.next(); System.out.println(next); } } }
當(dāng)使用Iterator對(duì)集合元素進(jìn)行迭代時(shí),Iterator并不是把集合元素本身傳給了迭代變量,而是把集合元素的值傳給了迭代變量,所以修改迭代變量的值對(duì)集合元素本身并沒(méi)有任何影響。
當(dāng)使用Iterator迭代訪問(wèn)Collection集合元素時(shí),Collection集合里的元素不能被改變,只有通過(guò)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迭代過(guò)程中,不可修改集合元素 collection.remove(e); } }); System.out.println(collection); } }
Iterator迭代器采用快速失敗機(jī)制fail-fast。一旦迭代過(guò)程中檢測(cè)到該集合已經(jīng)被修改,程序立即出發(fā)ConcurrentModificationException異常,而不是顯示修改后的結(jié)果,避免了共享資源而引發(fā)的潛在問(wèn)題。
使用Lambda表達(dá)式遍歷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表達(dá)式來(lái)遍歷集合 books.forEach((e) -> System.out.println(e)); } }
當(dāng)使用foreach循環(huán)迭代訪問(wèn)幾個(gè)元素時(shí),該集合也不能被改變。
使用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); } }
計(jì)數(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)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/69319.html
摘要:集合框架的基本接口類(lèi)層次結(jié)構(gòu)其中表示接口,表示實(shí)現(xiàn)類(lèi)和在實(shí)際開(kāi)發(fā)中,需要將使用的對(duì)象存儲(chǔ)于特定數(shù)據(jù)結(jié)構(gòu)的容器中。實(shí)例是迭代器,擁有兩個(gè)方法方法迭代器用于遍歷集合元素。返回值則是轉(zhuǎn)換后的數(shù)組,該數(shù)組會(huì)保存集合中的所有元素。 Java Collections Framework是Java提供的對(duì)集合進(jìn)行定義,操作,和管理的包含一組接口,類(lèi)的體系結(jié)構(gòu)。 Java集合框架的基本接口/類(lèi)層次結(jié)構(gòu)...
摘要:?jiǎn)尉€程集合本部分將重點(diǎn)介紹非線程安全集合。非線程安全集合框架的最新成員是自起推出的。這是標(biāo)準(zhǔn)的單線程陣營(yíng)中唯一的有序集合。該功能能有效防止運(yùn)行時(shí)造型。檢查個(gè)集合之間不存在共同的元素。基于自然排序或找出集合中的最大或最小元素。 【編者按】本文作者為擁有十年金融軟件開(kāi)發(fā)經(jīng)驗(yàn)的 Mikhail Vorontsov,文章主要概覽了所有標(biāo)準(zhǔn) Java 集合類(lèi)型。文章系國(guó)內(nèi) ITOM 管理平臺(tái) O...
摘要:如果需要?jiǎng)?chuàng)建對(duì)象,則必須與一個(gè)被迭代的集合。這是一個(gè)有狀態(tài)的方法該方法用于保證對(duì)該流的后續(xù)訪問(wèn)中最大允許訪問(wèn)的元素個(gè)數(shù)。可以對(duì)集合元素進(jìn)行整體的聚集操作。 Java集合分為Set(無(wú)序、不可重復(fù))、List(有序、重復(fù))、Queue(隊(duì)列)和Map(映射關(guān)系) Java集合概述 數(shù)組元素既可以是基本類(lèi)型的值,也可以是對(duì)象(實(shí)際保存對(duì)象的引用變量)集合只能保存對(duì)象(實(shí)際保存對(duì)象的引用變量...
集合介紹 本節(jié)介紹Java集合框架,在這里,你將了解集合是什么以及它們?nèi)绾问鼓愕墓ぷ鞲p松、程序更好,你將了解構(gòu)成Java集合框架的核心元素 — 接口、實(shí)現(xiàn)、聚合操作和算法。 集合 — 有時(shí)稱為容器 — 只是一個(gè)將多個(gè)元素組合到一個(gè)單元中的對(duì)象,集合用于存儲(chǔ)、檢索、操作和傳遞聚合數(shù)據(jù)。通常,它們代表形成自然組的數(shù)據(jù)項(xiàng),例如撲克牌(卡片集合)、郵件文件夾(信件集合)或電話目錄(名稱到電話號(hào)碼的映射)...
摘要:第三階段常見(jiàn)對(duì)象的學(xué)習(xí)集合框架概述和集合的遍歷一集合框架的概述集合的由來(lái)如果一個(gè)程序只包含固定數(shù)量的且其生命周期都是已知的對(duì)象,那么這是一個(gè)非常簡(jiǎn)單的程序。進(jìn)而它們的遍歷方式也應(yīng)該是不同的,最終就沒(méi)有定義迭代器類(lèi)。 第三階段 JAVA常見(jiàn)對(duì)象的學(xué)習(xí) 集合框架概述和集合的遍歷 (一) 集合框架的概述 (1) 集合的由來(lái) 如果一個(gè)程序只包含固定數(shù)量的且其生命周期都是已知的對(duì)象,那么這是一...
閱讀 2669·2021-09-09 09:33
閱讀 2822·2019-08-30 15:54
閱讀 2881·2019-08-30 14:21
閱讀 2368·2019-08-29 17:15
閱讀 3595·2019-08-29 16:13
閱讀 2773·2019-08-29 14:21
閱讀 3438·2019-08-26 13:25
閱讀 2040·2019-08-26 12:14