ArrayList的retainAll()方法用於刪除指定集合中未包含的所有數組列表元素,或保留當前ArrayList實例中與作為參數傳遞給該方法的集合列表中所有元素匹配的所有匹配元素。
用法:
public boolean retainAll(Collection C)
參數:C是包含要保留在列表中的元素的集合。
返回值:如果由於調用而導致列表完全更改,則該方法返回布爾值true,否則返回false。
異常:
- ClassCastException:如果此ArrayList的元素的類與Passed集合不兼容。這是可選的。
- NullPointerException:如果列表包含null元素,並且傳遞的集合不允許null元素,或者指定的collection為null。這也是可選的。
下麵的程序用於說明Java.util.ArrayList.retainAll()方法:
示例1:將ArrayList集合作為參數傳遞給方法。
// Java code to illustrate retainAll() method
import java.util.ArrayList;
public class GFG {
public static void main(String[] args)
{
// Creating an empty array list
ArrayList<String> bags = new ArrayList<String>();
// Add values in the bags list.
bags.add("pen");
bags.add("pencil");
bags.add("paper");
// Creating another array list
ArrayList<String> boxes = new ArrayList<String>();
// Add values in the boxes list.
boxes.add("pen");
boxes.add("paper");
boxes.add("books");
boxes.add("rubber");
// Before Applying method print both lists
System.out.println("Bags Contains :" + bags);
System.out.println("Boxes Contains :" + boxes);
// Apply retainAll() method to boxes passing bags as parameter
boxes.retainAll(bags);
// Displaying both the lists after operation
System.out.println("\nAfter Applying retainAll()"+
" method to Boxes\n");
System.out.println("Bags Contains :" + bags);
System.out.println("Boxes Contains :" + boxes);
}
}
輸出:
Bags Contains :[pen, pencil, paper] Boxes Contains :[pen, paper, books, rubber] After Applying retainAll() method to Boxes Bags Contains :[pen, pencil, paper] Boxes Contains :[pen, paper]
示例2:將與ArrayList不同的Collection作為參數傳遞給方法。
// Program Demonstrate retainAll() method With
// Collection different then ArrayList as a parameter of the method
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Creating an empty array list
HashSet<String> bags = new HashSet<String>();
// Add values in the bags Set.
bags.add("pen");
bags.add("ink");
bags.add("paper");
// Creating another empty array list
ArrayList<String> boxes = new ArrayList<String>();
// Add values in the boxes list.
boxes.add("pen");
boxes.add("paper");
boxes.add("books");
boxes.add("rubber");
boxes.add("ink");
// Before Applying method print both list and set.
System.out.println("Bags Contains :" + bags);
System.out.println("Boxes Contains :" + boxes);
// Apply retainAll() method to boxes passing bags as parameter
boxes.retainAll(bags);
// Displaying both the lists after operation
System.out.println("\nAfter Applying retainAll()" +
" method to Boxes\n");
System.out.println("Bags Contains :" + bags);
System.out.println("Boxes Contains :" + boxes);
}
}
輸出:
Bags Contains :[paper, ink, pen] Boxes Contains :[pen, paper, books, rubber, ink] After Applying retainAll() method to Boxes Bags Contains :[paper, ink, pen] Boxes Contains :[pen, paper, ink]
示例3:說明retainAll()方法引發的錯誤
// Program to illustrate error thrown by retainAll() method
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// Creating an empty array list
ArrayList<Integer> list1 = null;
/// Creating another empty array list
ArrayList<String> list2 = new ArrayList<String>();
// Add values in the list2 list.
list2.add("pen");
list2.add("paper");
list2.add("books");
list2.add("rubber");
// Before Applying method print both lists
System.out.println("list1 Contains :" + list1);
System.out.println("list2 Contains :" + list2);
// Apply retainAll() method to list2 passing list1 as parameter
list2.retainAll(list1);
// Displaying both the lists after operation
System.out.println("\nAfter Applying retainAll()"+
" method to list2\n");
System.out.println("list1 Contains :" + list1);
System.out.println("list2 Contains :" + list2);
}
}
輸出:
list1 Contains :null list2 Contains :[pen, paper, books, rubber]
運行時錯誤:
Exception in thread "main" java.lang.NullPointerException at java.util.Objects.requireNonNull(Objects.java:203) at java.util.ArrayList.retainAll(ArrayList.java:714) at GFG.main(GFG.java:26)
參考:https://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#retainAll(java.util.Collection)
相關用法
- Java Set retainAll()用法及代碼示例
- Java AbstractSequentialList retainAll()用法及代碼示例
- Java AbstractSet retainAll()用法及代碼示例
- Java TreeSet retainAll()用法及代碼示例
- Java LinkedHashSet retainAll()用法及代碼示例
- Java HashSet retainAll()用法及代碼示例
- Java Stack retainAll()用法及代碼示例
- Java CopyOnWriteArraySet retainAll()用法及代碼示例
- Java LinkedBlockingDeque retainAll()用法及代碼示例
- Java SortedSet retainAll()用法及代碼示例
- Java AbstractCollection retainAll()用法及代碼示例
- Java List retainAll()用法及代碼示例
- Java Vector retainAll()用法及代碼示例
- Java LinkedTransferQueue retainAll()用法及代碼示例
- Java CopyOnWriteArrayList retainAll()用法及代碼示例
注:本文由純淨天空篩選整理自AmanSingh2210大神的英文原創作品 ArrayList retainAll() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。