Java中的ConcurrentSkipListSet removeAll()方法

这个 removeAll() 方法 JAVAutil。同时发生的ConcurrentSkipListSet 是Java中的一个内置函数,它返回并从此集合中删除指定集合中包含的所有元素。如果指定的集合也是一个集合,则此操作会有效地修改该集合,使其值为两个集合的非对称集合差。

null

语法:

public boolean removeAll(Collection c)

参数: 该函数只接受一个参数 C

返回值: 如果此集合因调用而更改,则函数返回true。

例外情况: 该函数引发以下异常:

  • 类别例外 –如果该集合中一个或多个元素的类型与指定集合不兼容
  • 空指针异常 –如果指定的集合或其任何元素为空

    下面的程序演示了ConcurrentSkipListSet。removeAll()方法:

    项目1:

    // Java program to demonstrate removeAll()
    // method of ConcurrentSkipListSet
    import java.util.concurrent.*;
    import java.util.ArrayList;
    import java.util.List;
    class ConcurrentSkipListSetremoveAllExample1 {
    public static void main(String[] args)
    {
    // Initializing the List
    List<Integer> list = new ArrayList<Integer>();
    // Adding elements in the list
    for ( int i = 1 ; i <= 10 ; i += 2 )
    list.add(i);
    // Contents of the list
    System.out.println( "Contents of the list: " + list);
    // Initializing the set
    ConcurrentSkipListSet<Integer>
    set = new ConcurrentSkipListSet<Integer>();
    // Adding elements in the set
    for ( int i = 1 ; i <= 10 ; i++)
    set.add(i);
    // Contents of the set
    System.out.println( "Contents of the set: " + set);
    // Remove all elements from the set which are in the list
    set.removeAll(list);
    // Contents of the set after removal
    System.out.println( "Contents of the set after removal: "
    + set);
    }
    }

    
    

    输出:

    Contents of the list: [1, 3, 5, 7, 9]
    Contents of the set: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    Contents of the set after removal: [2, 4, 6, 8, 10]
    

    项目2: 在removeAll()中显示NullPOinterException的程序。

    // Java program to demonstrate removeAll()
    // method of ConcurrentSkipListSet
    import java.util.concurrent.*;
    import java.util.ArrayList;
    import java.util.List;
    class ConcurrentSkipListSetremoveAllExample1 {
    public static void main(String[] args)
    {
    // Initializing the set
    ConcurrentSkipListSet<Integer>
    set = new ConcurrentSkipListSet<Integer>();
    // Adding elements in the set
    for ( int i = 1 ; i <= 10 ; i++)
    set.add(i);
    // Contents of the set
    System.out.println( "Contents of the set: " + set);
    try {
    // Remove all elements from the set which are null
    set.removeAll( null );
    }
    catch (Exception e) {
    System.out.println( "Exception: " + e);
    }
    }
    }

    
    

    输出:

    Contents of the set: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    Exception: java.lang.NullPointerException
    

    参考:

    https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentSkipListSet.html#removeAll-爪哇。util。收藏-

  • © 版权声明
    THE END
    喜欢就支持一下吧
    点赞13 分享