CodeGym /Java Blog /Toto sisi /Java 中的 ArrayList removeAll() 方法
John Squirrels
等級 41
San Francisco

Java 中的 ArrayList removeAll() 方法

在 Toto sisi 群組發布
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