CodeGym /Java 博客 /随机的 /Java 中的 ArrayList removeAll() 方法
John Squirrels
第 41 级
San Francisco

Java 中的 ArrayList removeAll() 方法

已在 随机的 群组中发布
Java 中的ArrayList removeAll()方法不会从适当的ArrayList中删除所有元素,正如其名称所暗示的那样。如果需要此操作,请使用 ArrayList 的clear()方法。与clear()不同,removeAll()方法从给定列表中删除另一个集合中包含的所有项目。

removeAll() 语法和声明

这是方法语法:

boolean removeAll(Collection<?> c)
其中c是一个集合,其中包含要从此列表中删除的元素。如果此列表因调用而更改,则此方法返回 true。它还可以抛出异常: ClassCastException — 如果此列表的元素的类与指定的集合不兼容 NullPointerException — 如果列表包含 null 元素并且指定的集合不允许 null 元素,或者如果指定的集合为 null

removeAll() 代码示例

假设您在社交网络上有一个好友列表。好吧,这是目前的常态。另外,假设您是一个一丝不苟的人,并且将所有不怀好意的人都列入了一个特殊的敌人名单,而不管您的敌人是否在您的社交网络好友中。您已决定清除所有恶意的朋友列表。removeAll ()方法肯定会对此有所帮助:

import java.util.ArrayList;
import java.util.List;

public class RemoveAll2Example {
  
       public static void main(String[] args) {

           
           List<String> friendList = new ArrayList<String>();

           // use add() method to add friends into your friend list
           friendList.add("Alex");
           friendList.add("Ivy");
           friendList.add("Victor");
           friendList.add("Peter");
           friendList.add("Lenny");
           friendList.add("Olly");
           friendList.add("Stu");
           friendList.add("Scott");
           friendList.add("Olivia");

           System.out.println("My old friend list: "+ friendList);

           //create and add elements to foe list 
           List<String> foeList = new ArrayList<String>();

           foeList.add("Ben");
           foeList.add("Scott");
           foeList.add("Chuck");
           foeList.add("Olly");
           foeList.add("Sam");
           System.out.println("All my enemies: "+ foeList);

           //remove all foeList elements from friendList list if they exist
           friendList.removeAll(foeList);
           System.out.println("new friend List without foes: "+ friendList);
       }
   }
输出是:
我的老朋友列表:[Alex, Ivy, Victor, Peter, Lenny, Olly, Stu, Scott, Olivia] 我所有的敌人:[Ben, Scott, Chuck, Olly, Sam] 没有敌人的新朋友列表:[Alex, Ivy, Victor、Peter、Lenny、Stu、Olivia]
如您所见,该方法删除了您的敌人 Olly 和 Scott。你的好友列表中没有敌人 Ben、Chuck 和 Sam。您可能已经注意到,根据removeAll()方法声明,您不仅可以从列表中删除另一个列表中的值,还可以从随机集合中删除值。让我们以HashMap为例。在这种情况下,您需要明确指出您要删除那些与值(removeAll (collection.values())或键(removeAll (collection.keySet()))匹配的记录。假设我们有一个HashMap包含我们朋友中所有女孩的名字,我们需要在 friendList 中只留下男孩(我不知道为什么可能需要这个,但你永远不知道)。让我们回到我们的旧朋友列表示例,创建一个女孩HasMap并从朋友列表中删除所有名字写在我们HashMap的值中的女孩。

import java.util.*;

public class RemoveAll2Example {
  
       public static void main(String[] args) {

          // create an empty array list and full it up using Java 9 Factory Methods  in one line
List<String> friendList= new ArrayList<>(List.of("Alex", "Ivy", "Victor", "Peter", "Lenny", "Olly", "Stu", "Scott", "Olivia"));

           System.out.println("My old friend list: "+ friendList);

           // Creating hashMap with girls keys and names also using using Java 9 Factory Methods   
           Map<Integer, String> girls = new HashMap<>(Map.of(1, "Olly", 2, "Olivia", 3, "Loe", 4, "Ivy"));
           friendList.removeAll(girls.values());
           System.out.println("boys only friendList: " + friendList);
       }
   }
这是输出:
我的老朋友列表:[Alex, Ivy, Victor, Peter, Lenny, Olly, Stu, Scott, Olivia] 仅限男孩朋友列表:[Alex, Victor, Peter, Lenny, Stu, Scott]

一些特殊情况

在本文的开头,我写道要从列表中删除它的所有值,您需要使用 ArrayList clear()方法。是的,当然你也可以为此使用removeAll()方法。您可能已经猜到如何执行此操作:

myList.RemoveAll (myList);
让我们举个例子。在反社会的情况下,让我们清空整个 friendList:

 import java.util.*;

public class RemoveAll2Example {
  
       public static void main(String[] args) {

           // create an empty array list and full it up using  Java 9 Factory Methods  in one line
           List<String> friendList= new ArrayList<>(List.of("Alex", "Ivy", "Victor", "Peter", "Lenny", "Olly", "Stu", "Scott", "Olivia"));
           System.out.println("My old friend list: "+ friendList);

           friendList.removeAll(friendList);
           System.out.println("my new empty friendlist...:" + friendList);
       }
   }
这是输出:
只有男孩的朋友列表:[Alex, Victor, Peter, Lenny, Stu, Scott] 我新的空朋友列表...:[]
至少让我们尝试从ArrayList 中移除空集合元素:

import java.util.*;

public class RemoveAll2Example {
  
       public static void main(String[] args) {

           List<String> friendList = new ArrayList<>(List.of("Alex", "Ivy", "Victor", "Peter", "Lenny", "Olly", "Stu", "Scott", "Olivia"));
           System.out.println("My old friend list: "+ friendList);

           // we are happy not to have foes 
           List<String> foeList = null;
          
           //trying to remove all foeList elements from friendList list 
            friendList.removeAll(foeList);
            System.out.println("my new friendlist:" + friendList);
       }
   }
根据removeAll()方法定义,我们在输出中得到了一个异常:
我的老朋友列表:[Alex, Ivy, Victor, Peter, Lenny, Olly, Stu, Scott, Olivia] thread "main" java.lang.NullPointerException 异常
评论
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION