java集合Collection常用方法解讀
前言
出去面試的時(shí)候,對(duì)java的集合框架考察的知識(shí)點(diǎn)還是蠻多的。除了基礎(chǔ)的集合常見(jiàn)API使用,對(duì)集合底層的實(shí)現(xiàn)原理以及數(shù)據(jù)結(jié)構(gòu)等也有很多考察方面。而自己對(duì)這方面知之甚少,特地抽空進(jìn)行研究和學(xué)習(xí)一下。
為什么要有集合
提到集合就不得不提一下數(shù)組,好多集合底層都是依賴于數(shù)組的實(shí)現(xiàn)。數(shù)組一旦初始化后,長(zhǎng)度就確定了,存儲(chǔ)數(shù)據(jù)對(duì)象不能達(dá)到動(dòng)態(tài)擴(kuò)展,其次數(shù)組存儲(chǔ)元素不便于對(duì)數(shù)組進(jìn)行添加、修改、刪除操作,而且數(shù)組可以存儲(chǔ)重復(fù)元素。
這個(gè)時(shí)候集合對(duì)作用顯現(xiàn)出來(lái)了。
集合分為Collection
和Map
兩種體系。
下面先介紹Collection的集合類的繼承樹(shù)如下圖所示:
Collection接口是 (java.util.Collection)是Java集合類的頂級(jí)接口之一,整個(gè)集合框架就圍繞一組標(biāo)準(zhǔn)接口而設(shè)計(jì),本文研究的集合基于JDK8的實(shí)現(xiàn),下面將會(huì)進(jìn)行多個(gè)集合類方法使用和主要方法源碼分析,以作為后續(xù)出去面試的參考資料。
Collection方法接口介紹
Collection 接口有 3 種子類型集合: List
、Set
和 Queue
,再下面是一些抽象類,最后是具體實(shí)現(xiàn)類,常用的有 ArrayList、LinkedList、HashSet、LinkedHashSet、ArrayBlockingQueue等,下面是Collection的所有方法。
這些方法即可以操作Set集合,也可以操作Queue和List集合,下面分別使用Collection
集合接口的方法說(shuō)明
方法名 | 說(shuō)明 |
---|---|
boolean add(E e) | 向集合添加元素e,若指定集合元素改變了則返回true |
boolean addAll(Collection<? extends E> c) | 把集合C中的元素全部添加到集合中,若指定集合元素改變返回true |
void clear() | 清空所有集合元素 |
boolean contains(Object o) | 判斷指定集合是否包含對(duì)象o |
boolean containsAll(Collection<?> c) | 判斷指定集合是否包含集合c的所有元素 |
boolean isEmpty() | 判斷指定集合的元素size是否為0 |
boolean remove(Object o) | 刪除集合中的元素對(duì)象o,若集合有多個(gè)o元素,則只會(huì)刪除第一個(gè)元素 |
boolean removeAll(Collection<?> c) | 刪除指定集合包含集合c的元素 |
boolean retainAll(Collection<?> c) | 從指定集合中保留包含集合c的元素,其他元素則刪除 |
int size() | 集合的元素個(gè)數(shù) |
T[] toArray(T[] a) | 將集合轉(zhuǎn)換為T(mén)類型的數(shù)組 |
下面是主要方法的演示:
@Test @SuppressWarnings("all") public void testCollection() { // 創(chuàng)建Collection接口的實(shí)現(xiàn) Collection collection = new ArrayList<>(); // 添加元素 collection.add("嘻嘻"); String src = "????"; collection.add(src); System.out.println(collection); // 創(chuàng)建Collection的實(shí)現(xiàn) Collection<String> coll = new HashSet<>(); coll.add("?"); coll.add("?"); coll.add("?"); System.out.println(coll); // 添加一個(gè)集合數(shù)據(jù) collection.addAll(coll); // 輸出集合的長(zhǎng)度 System.out.println(collection); // 判斷是否包含 System.out.println(collection.contains("?")); // 移除元素 collection.remove("?"); // 添加對(duì)象 collection.add(new Person("張三", 23, 5000d)); // 當(dāng)認(rèn)為兩個(gè)對(duì)象屬性一致,相等時(shí)候,需重寫(xiě)hashCode 和 equals方法 System.out.println(collection.contains(new Person("張三", 23, 5000d))); System.out.println("-------"); collection.add(null); Collection<String> collection1 = new ArrayList<>(); collection1.add("嘻嘻"); collection1.add("?"); // 求兩個(gè)集合的交集(只保留collection1存在的元素) collection.retainAll(collection1); System.out.println(collection); // 清空元素 collection.clear(); System.out.println(collection); }
java8新特性操作集合
使用lambda表達(dá)式遍歷集合
java8為Collection
的父接口(Iterable
)提供了一個(gè)默認(rèn)的Foreach
方法,我們可以使用它進(jìn)行集合遍歷,若對(duì)lambda不太了解,不妨訪問(wèn)一下這篇文章? lambda表達(dá)式學(xué)習(xí)
@Test public void testForeach() { Collection<String> collection = new ArrayList<>(); collection.add("i"); collection.add("love"); collection.add("china"); // foreach遍歷 collection.forEach(e-> System.out.println(e)); // 可以使用方法引用簡(jiǎn)寫(xiě) collection.forEach(System.out::println); // 或者迭代器的forEachRemaining方法 collection.iterator().forEachRemaining(System.out::println); }
使用java8的predicate操作集合
@Test public void testPredicate() { Collection<Integer> collection = new ArrayList<>(); // 添加0-49 for (int i = 0; i < 50; i++) { collection.add(i); } // 移除10-49的數(shù)字 collection.removeIf(e -> (e > 9 && e < 50)); System.out.println(collection);// 輸出[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] }
基于流操作集合
java8之后引入了Stream
相關(guān)流操作java集合,通過(guò)流大大簡(jiǎn)化了對(duì)集合操作,關(guān)于這些流式操作,可以查看這篇文章? Stream入門(mén)學(xué)習(xí),下面是基于流的一些簡(jiǎn)單演示:
@Test public void testIntStream() { Collection<Integer> collection = new ArrayList<>(); Random random = new Random(); for (int i = 0; i < 10; i++) { collection.add(random.nextInt(100)); } System.out.println(collection); // collection存儲(chǔ)的數(shù)值是包裝類型,可以將其轉(zhuǎn)換為IntStream IntStream intStream = collection.stream().mapToInt(e -> e); // intStream.forEach(System.out::println); System.out.println(collection.stream().mapToInt(e -> e).sum()); // 輸出最大值 collection.stream().mapToInt(e -> e).max().ifPresent(System.out::println); // 輸出最小值 collection.stream().mapToInt(e -> e).min().ifPresent(System.out::println); // 統(tǒng)計(jì)大于50的數(shù) System.out.println(collection.stream().filter(e -> e > 50).count()); // 原集合每一個(gè)值加1 collection.stream().mapToInt(e-> e+1).forEach(System.out::println); // 排序 collection.stream().mapToInt(e-> e).sorted().forEach(System.out::println); // 原數(shù)值每一個(gè)元素?cái)U(kuò)大2倍 int[] ints = collection.stream().mapToInt(e -> e << 1).toArray(); // 輸出原數(shù)組 System.out.println(Arrays.toString(ints)); // 將數(shù)組轉(zhuǎn)流 IntStream stream = Arrays.stream(ints); // 輸出流平均數(shù) System.out.println(stream.average().getAsDouble()); }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot如何進(jìn)行業(yè)務(wù)校驗(yàn)實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于SpringBoot如何進(jìn)行業(yè)務(wù)校驗(yàn)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-01-01Java如何利用狀態(tài)模式(state pattern)替代if else
這篇文章主要給大家介紹了關(guān)于Java如何利用狀態(tài)模式(state pattern)替代if else的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Spring Boot如何使用httpcomponents實(shí)現(xiàn)http請(qǐng)求
這篇文章主要介紹了Spring Boot使用httpcomponents實(shí)現(xiàn)http請(qǐng)求的示例代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-07-07在java中使用SPI創(chuàng)建可擴(kuò)展的應(yīng)用程序操作
這篇文章主要介紹了在java中使用SPI創(chuàng)建可擴(kuò)展的應(yīng)用程序操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09