开发者

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

目录
  • 前言
  • 1. containsAll()—— 检查是否完全包含另一个集合
    • 示例:使用containsAll()
    • 输出结果:
  • 2. addAll()—— 将另一个集合的元素添加进来(并集操作)
    • 示例:使用addAll()
    • 输出结果:
    • 小提示:
  • 3. removeAll()—— 移除另一个集合中存在的元素(补集操作)
    • 示例:使用removeAll()
    • 输出结果:
  • 4. retainAll()—— 保留两者共有的元素(交集操作)
    • 示例:使用retainAll()
    • 输出结果:
  • 总结

    前言

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

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

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

    方法集合运算作用
    containsAll(Collection<?> c)包含(包含关系)判断当前集合是否包含另一个集合的所有元素
    addAll(Collection<? extends E> c)并集(union)将另一个集合的所有元素添加到当前集合
    removeAll(Collection<?> c)补集(complement)移除当前集合中出现在另一个集合中的所有元素
    retainAll(Collection<?> c)交集(inter编程section)仅保留当前集合中也存在于另一个集合中的元素

    1. containsAll()—&mdash编程客栈; 检查是否完全包含另一个集合

    • 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表示:当前集合发生了变化(不代表全部成功javascript添加,只要有变化就返回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操作集合多个元素内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!

    0

    上一篇:

    下一篇:

    精彩评论

    暂无评论...
    验证码 换一张
    取 消

    最新开发

    开发排行榜