boolean
removeAll(Collection<?> c)
Removes
from this list all of its elements that are contained in the
specified collection (optional operation).
import java.util.*; class ListRemoveAll{ public static void main(String args[]){ List<Integer> myList = new ArrayList<> (); Set<Integer> mySet =new HashSet<> (); /* Add Elements to myList */ for(int i=0; i < 10; i++){ myList.add(i); } /* Add Elements to mySet */ for(int i=5; i < 15; i++){ mySet.add(i); } System.out.println("Elements in myList are "); System.out.println(myList +"\n"); System.out.println("Elements in mySet are "); System.out.println(mySet +"\n"); /* Remove mySet Data from myList */ myList.removeAll(mySet); System.out.println("Elements in myList after removing mySet are"); System.out.println(myList); } }
Output
Elements in myList are [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Elements in mySet are [5, 6, 7, 8, 9, 10, 11, 12, 13, 14] Elements in myList after removing mySet are [0, 1, 2, 3, 4]
1.
throws UnsupportedOperationException if the removeAll operation
is
not supported by this list.
import java.util.*; class ListRemoveAllUnsupport{ public static void main(String args[]){ List<Integer> myList = new ArrayList<> (); List<Integer> unmodifiable; Set<Integer> mySet =new HashSet<> (); /* Add Elements to myList */ for(int i=0; i < 10; i++){ myList.add(i); } /* Add Elements to mySet */ for(int i=5; i < 15; i++){ mySet.add(i); } unmodifiable = Collections.unmodifiableList(myList); System.out.println("Elements in unmodifiable list are "); System.out.println(unmodifiable +"\n"); System.out.println("Elements in mySet are "); System.out.println(mySet +"\n"); /* Remove mySet Data from unmodifiable list */ unmodifiable.removeAll(mySet); System.out.println("Elements in unmodifiable after removing mySet are"); System.out.println(unmodifiable); } }
Output
Elements in unmodifiable list are [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Elements in mySet are [5, 6, 7, 8, 9, 10, 11, 12, 13, 14] Exception in thread "main" java.lang.UnsupportedOperationException at java.util.Collections$UnmodifiableCollection.removeAll(Unknown Source) at ListRemoveAllUnsupport.main(ListRemoveAllUnsupport.java:26)
2. throws
ClassCastException if the class of an element of this list is
incompatible with the specified collection
3.
throws NullPointerException if this list contains a null element and
the specified
collection does not permit null elements or if the specified
collection is null
No comments:
Post a Comment