Java中的ArrayDeque removeAll()方法

这个 removeAll() ArrayDeque方法用于删除ArrayDeque和作为参数传递的集合中常见的所有元素。此方法首先收集集合的所有元素,然后开始从ArrayQue中删除与集合元素相同的元素。执行此方法后,此ArrayQueu将不包含与集合相同的元素。若调用此方法导致此ArrayQue发生更改,则此方法为True。

null

语法:

public boolean removeAll(Collection<? extends E> col)

参数: 此方法接受一个参数 C 它代表了我们想要从这个deque中删除的元素的集合。

返回: 此方法返回 符合事实的 如果调用此方法导致此数据更改。

例外情况: 这个方法抛出 空指针异常 如果指定的集合或其任何元素为空。

下面的程序演示了ArrayQue的removeAll()方法: 项目1: 程序演示ArrayQue上的removeAll()方法,该方法将从包含数字的集合中删除与元素相同的元素。

// Java Program Demonstrate removeAll()
// method of ArrayDeque
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create an ArrayDeque which going to
// contains a list of Numbers
ArrayDeque<Integer> Numbers = new ArrayDeque<Integer>();
// Add Number to list
Numbers.add( 23 );
Numbers.add( 32 );
Numbers.add( 45 );
Numbers.add( 63 );
// print ArrayDeque before calling removeAll()
System.out.println( "Before calling removeAll()" );
print(Numbers);
// create a collection of Number to
// remove elements from ArrayDeque using removeAll()
ArrayList<Integer> col = new ArrayList<Integer>();
// add Numbers to collection
col.add( 45 );
col.add( 44 );
col.add( 63 );
// remove Numbers same as elements of collection
// from ArrayDeque using removeAll()
Numbers.removeAll(col);
// print ArrayDeque after calling removeAll()
System.out.println( "After calling removeAll()" );
print(Numbers);
}
// printing all elements of ArrayDeque
public static void print(ArrayDeque<Integer> arDe)
{
arDe.forEach((n) -> System.out.print(n + " " ));
System.out.println();
}
}


输出:

Before calling removeAll()
23 32 45 63 
After calling removeAll()
23 32

项目2: 程序演示ArrayQue上的removeAll()方法,该方法将从学生姓名集合中删除与元素相同的元素。

// Java Program Demonstrate removeAll()
// method of ArrayDeque
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create an ArrayDeque which going to
// contains a list of Student names which is actually
// string values
ArrayDeque<String> students = new ArrayDeque<String>();
// Add Strings to list
// each string represents student name
students.add( "Ram" );
students.add( "Mohan" );
students.add( "Sohan" );
students.add( "Rabi" );
// print ArrayDeque before calling removeAll()
System.out.println( "Before calling removeAll()" );
print(students);
// create a collection of Number to
// remove elements from ArrayDeque using removeAll()
LinkedList<String> col = new LinkedList<String>();
// add Names in collection
col.add( "Rabi" );
col.add( "Sohan" );
// remove the elements same as collection
// from ArrayDeque using removeAll()
students.removeAll(col);
// print ArrayDeque
System.out.println( "After calling removeAll()" );
print(students);
}
// printing all elements of ArrayDeque
public static void print(ArrayDeque<String> arDe)
{
System.out.println( "List of Students Name:" );
arDe.forEach((n) -> System.out.print( " | " + n + " | " ));
System.out.println( "" );
}
}


输出:

Before calling removeAll()
List of Students Name:
 | Ram |  | Mohan |  | Sohan |  | Rabi | 

After calling removeAll()
List of Students Name:
 | Ram |  | Mohan |

方案3: 用于演示removeAll()方法引发的异常的程序。

// Java Program Demonstrate Exception thrown by removeAll()
// method of ArrayDeque
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// create an ArrayDeque which going to
// contains a list of Numbers
ArrayDeque<Integer> Numbers = new ArrayDeque<Integer>();
// Add Number to list
Numbers.add( 223 );
Numbers.add( 132 );
Numbers.add( 345 );
Numbers.add( 563 );
// create a collection of Number which is null
ArrayList<Integer> col = null ;
try {
// call removeAll() method
Numbers.removeAll(col);
}
catch (Exception e) {
System.out.println(e);
}
}
}


输出:

java.lang.NullPointerException

参考: https://docs.oracle.com/javase/10/docs/api/java/util/ArrayDeque.html#removeAll(java.util.Collection)

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