📌  相关文章
📜  Java中的 ConcurrentSkipListSet removeAll() 方法

📅  最后修改于: 2022-05-13 01:55:23.859000             🧑  作者: Mango

Java中的 ConcurrentSkipListSet removeAll() 方法

Java .util.concurrent.ConcurrentSkipListSetremoveAll()方法是Java中的一个内置函数,它返回从该集合中删除的所有包含在指定集合中的元素。如果指定的集合也是一个集合,这个操作有效地修改了这个集合,使得它的值是两个集合的不对称集合差。

句法:

public boolean removeAll(Collection c)

参数:函数接受单个参数c

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

异常:该函数抛出以下异常:

  • ClassCastException – 如果此集合中的一个或多个元素的类型与指定的集合不兼容
  • NullPointerException – 如果指定的集合或其任何元素为空

    下面的程序说明了 ConcurrentSkipListSet.removeAll() 方法:

    方案一:

    // 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 list = new ArrayList();
      
            // 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
                set = new ConcurrentSkipListSet();
      
            // 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
                set = new ConcurrentSkipListSet();
      
            // 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 Java