Java如何操作集合中的多個元素(批量操作)
前言
在前面我們學習了針對單個元素的基本操作,
接下來,我們看看如何對一組元素進行批量處理!
Java 的 Collection 接口提供了四個非常重要的方法,它們分別對應了集合的四大基本運算:
| 方法 | 集合運算 | 作用 |
|---|---|---|
containsAll(Collection<?> c) | 包含(包含關系) | 判斷當前集合是否包含另一個集合的所有元素 |
addAll(Collection<? extends E> c) | 并集(union) | 將另一個集合的所有元素添加到當前集合 |
removeAll(Collection<?> c) | 補集(complement) | 移除當前集合中出現(xiàn)在另一個集合中的所有元素 |
retainAll(Collection<?> c) | 交集(intersection) | 僅保留當前集合中也存在于另一個集合中的元素 |
1. containsAll()—— 檢查是否完全包含另一個集合
containsAll(Collection<?> c)方法會返回true,如果當前集合包含了另一個集合的所有元素。- 兩個集合的類型可以不同,比如:可以檢查一個
Collection<String>是否包含一個Collection<User>的元素。
示例:使用containsAll()
import java.util.*;
public class ContainsAllExample {
public static void main(String[] args) {
Collection<String> strings = new ArrayList<>();
strings.add("one");
strings.add("two");
strings.add("three");
Collection<String> first = new ArrayList<>();
first.add("one");
first.add("two");
Collection<String> second = new ArrayList<>();
second.add("one");
second.add("four");
System.out.println("Is first contained in strings? " + strings.containsAll(first));
System.out.println("Is second contained in strings? " + strings.containsAll(second));
}
}
輸出結果:
Is first contained in strings? true Is second contained in strings? false
2. addAll()—— 將另一個集合的元素添加進來(并集操作)
addAll(Collection<? extends E> c)將傳入集合的所有元素添加到當前集合。- 返回值
true表示:當前集合發(fā)生了變化(不代表全部成功添加,只要有變化就返回true)。
示例:使用addAll()
import java.util.*;
public class AddAllExample {
public static void main(String[] args) {
Collection<String> strings = new ArrayList<>();
strings.add("one");
strings.add("two");
strings.add("three");
Collection<String> first = new ArrayList<>();
first.add("one");
first.add("four");
boolean hasChanged = strings.addAll(first);
System.out.println("Has strings changed? " + hasChanged);
System.out.println("strings = " + strings);
}
}
輸出結果:
Has strings changed? true strings = [one, two, three, one, four]
小提示:
如果使用HashSet等不允許重復元素的集合,addAll()的行為會不同——不會添加重復元素!
3. removeAll()—— 移除另一個集合中存在的元素(補集操作)
removeAll(Collection<?> c)方法會刪除當前集合中所有在傳入集合中也存在的元素。- 返回值
true表示集合有變化。
示例:使用removeAll()
import java.util.*;
public class RemoveAllExample {
public static void main(String[] args) {
Collection<String> strings = new ArrayList<>();
strings.add("one");
strings.add("two");
strings.add("three");
Collection<String> toBeRemoved = new ArrayList<>();
toBeRemoved.add("one");
toBeRemoved.add("four");
boolean hasChanged = strings.removeAll(toBeRemoved);
System.out.println("Has strings changed? " + hasChanged);
System.out.println("strings = " + strings);
}
}
輸出結果:
Has strings changed? true strings = [two, three]
4. retainAll()—— 保留兩者共有的元素(交集操作)
retainAll(Collection<?> c)方法只保留當前集合中也在傳入集合中出現(xiàn)的元素,其他的都移除。- 返回值
true表示集合內容發(fā)生了變化。
示例:使用retainAll()
import java.util.*;
public class RetainAllExample {
public static void main(String[] args) {
Collection<String> strings = new ArrayList<>();
strings.add("one");
strings.add("two");
strings.add("three");
Collection<String> toBeRetained = new ArrayList<>();
toBeRetained.add("one");
toBeRetained.add("four");
boolean hasChanged = strings.retainAll(toBeRetained);
System.out.println("Has strings changed? " + hasChanged);
System.out.println("strings = " + strings);
}
}
輸出結果:
Has strings changed? true strings = [one]
總結
| 方法 | 說明 | 示例 |
|---|---|---|
containsAll() | 檢查包含關系 | list.containsAll(subList) |
addAll() | 合并兩個集合(并集) | list.addAll(otherList) |
removeAll() | 移除交集元素(補集) | list.removeAll(otherList) |
retainAll() | 只保留交集元素(交集) | list.retainAll(otherList) |
到此這篇關于Java如何操作集合中的多個元素的文章就介紹到這了,更多相關Java操作集合多個元素內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Docker 和 Kubernetes用于容器化與編排Java 應用的最佳實踐
本文介紹了Docker和Kubernetes用于Java應用的最佳實踐,通過實戰(zhàn)案例展示了如何部署Java應用到Kubernetes集群,本文介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2026-04-04
SpringBoot 下的 Static 文件夾打包成前端資源的示例代碼
這篇文章主要介紹了SpringBoot 下的 Static 文件夾如何打包成前端資源,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06
spring boot在啟動項目之后執(zhí)行的實現(xiàn)方法
在開發(fā)時有時候需要在整個應用開始運行時執(zhí)行一些特定代碼,比如初始化環(huán)境,下面這篇文章就來給大家介紹了關于spring boot在啟動項目之后執(zhí)行自己要執(zhí)行的東西的實現(xiàn)方法,文中給出了詳細的示例代碼,需要的朋友可以參考下。2017-09-09
Java 實戰(zhàn)項目之倉庫管理系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SSM+jsp+mysql+maven實現(xiàn)一個倉庫管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11

