文章目录
- [229. Java 集合 - 操作集合中的多个元素(批量操作)](#229. Java 集合 - 操作集合中的多个元素(批量操作))
- [1. ✅ `containsAll()` ------ 检查是否完全包含另一个集合](#1. ✅
containsAll()—— 检查是否完全包含另一个集合) -
-
- [🛠️ 示例:使用 `containsAll()`](#🛠️ 示例:使用
containsAll()) - [🖨️ 输出结果:](#🖨️ 输出结果:)
- [🛠️ 示例:使用 `containsAll()`](#🛠️ 示例:使用
-
- [2. ➕ `addAll()` ------ 将另一个集合的元素添加进来(并集操作)](#2. ➕
addAll()—— 将另一个集合的元素添加进来(并集操作)) -
-
- [🛠️ 示例:使用 `addAll()`](#🛠️ 示例:使用
addAll()) - [🖨️ 输出结果:](#🖨️ 输出结果:)
- [📌 小提示:](#📌 小提示:)
- [🛠️ 示例:使用 `addAll()`](#🛠️ 示例:使用
-
- [3. ➖ `removeAll()` ------ 移除另一个集合中存在的元素(补集操作)](#3. ➖
removeAll()—— 移除另一个集合中存在的元素(补集操作)) -
-
- [🛠️ 示例:使用 `removeAll()`](#🛠️ 示例:使用
removeAll()) - [🖨️ 输出结果:](#🖨️ 输出结果:)
- [🛠️ 示例:使用 `removeAll()`](#🛠️ 示例:使用
-
- [4. ✨ `retainAll()` ------ 保留两者共有的元素(交集操作)](#4. ✨
retainAll()—— 保留两者共有的元素(交集操作)) -
-
- [🛠️ 示例:使用 `retainAll()`](#🛠️ 示例:使用
retainAll()) - [🖨️ 输出结果:](#🖨️ 输出结果:)
- [🛠️ 示例:使用 `retainAll()`](#🛠️ 示例:使用
-
- 🎤总结
229. Java 集合 - 操作集合中的多个元素(批量操作)
在前面我们学习了针对单个元素 的基本操作,
接下来,我们看看如何对一组元素进行批量处理!
Java 的 Collection 接口提供了四个非常重要的方法,它们分别对应了集合的四大基本运算:
| 方法 | 集合运算 | 作用 |
|---|---|---|
containsAll(Collection<?> c) |
包含(包含关系) | 判断当前集合是否包含另一个集合的所有元素 |
addAll(Collection<? extends E> c) |
并集(union) | 将另一个集合的所有元素添加到当前集合 |
removeAll(Collection<?> c) |
补集(complement) | 移除当前集合中出现在另一个集合中的所有元素 |
retainAll(Collection<?> c) |
交集(intersection) | 仅保留当前集合中也存在于另一个集合中的元素 |
1. ✅ containsAll() ------ 检查是否完全包含另一个集合
containsAll(Collection<?> c)方法会返回true,如果当前集合包含了另一个集合的所有元素。- 两个集合的类型可以不同,比如:可以检查一个
Collection<String>是否包含一个Collection<User>的元素。
🛠️ 示例:使用 containsAll()
java
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));
}
}
🖨️ 输出结果:
java
Is first contained in strings? true
Is second contained in strings? false
2. ➕ addAll() ------ 将另一个集合的元素添加进来(并集操作)
addAll(Collection<? extends E> c)将传入集合的所有元素添加到当前集合。- 返回值
true表示:当前集合发生了变化(不代表全部成功添加,只要有变化就返回true)。
🛠️ 示例:使用 addAll()
java
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);
}
}
🖨️ 输出结果:
java
Has strings changed? true
strings = [one, two, three, one, four]
📌 小提示:
如果使用HashSet等不允许重复 元素的集合,addAll()的行为会不同------不会添加重复元素!
3. ➖ removeAll() ------ 移除另一个集合中存在的元素(补集操作)
removeAll(Collection<?> c)方法会删除当前集合中所有在传入集合中也存在的元素。- 返回值
true表示集合有变化。
🛠️ 示例:使用 removeAll()
java
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);
}
}
🖨️ 输出结果:
java
Has strings changed? true
strings = [two, three]
4. ✨ retainAll() ------ 保留两者共有的元素(交集操作)
retainAll(Collection<?> c)方法只保留 当前集合中也在传入集合中出现的元素,其他的都移除。- 返回值
true表示集合内容发生了变化。
🛠️ 示例:使用 retainAll()
java
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);
}
}
🖨️ 输出结果:
java
Has strings changed? true
strings = [one]
🎤总结
| 方法 | 说明 | 示例 |
|---|---|---|
containsAll() |
检查包含关系 | list.containsAll(subList) |
addAll() |
合并两个集合(并集) | list.addAll(otherList) |
removeAll() |
移除交集元素(补集) | list.removeAll(otherList) |
retainAll() |
只保留交集元素(交集) | list.retainAll(otherList) |