Java如何操作集合中的多个元素(批量操作)

 更新时间:2025年11月10日 09:48:51   作者:yaoxin521123  
Java集合框架是一个统一架构,用于表示和操作集合,它提供了一套完整的接口和实现类,用于表示和操作各种集合,这篇文章主要介绍了Java如何操作集合中多个元素的相关资料,需要的朋友可以参考下

前言

在前面我们学习了针对单个元素的基本操作,

接下来,我们看看如何对一组元素进行批量处理!

JavaCollection 接口提供了四个非常重要的方法,它们分别对应了集合的四大基本运算

方法集合运算作用
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()

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表示:当前集合发生了变化(不代表全部成功添加,只要有变化就返回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) 方法只保留当前集合中也在传入集合中出现的元素,其他的都移除。
  • 返回值true表示集合内容发生了变化。

示例:使用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操作集合多个元素内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

最新评论