-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathArrayListRemoveAllExample.java
49 lines (40 loc) · 1.37 KB
/
ArrayListRemoveAllExample.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.gaurav.ExProject.ArrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
/**
* A java program showing an example for the removeAll()
* method of the ArrayList class in Java.
*
* @author gaurav
*/
public class ArrayListRemoveAllExample {
public static void main(String[] args) {
// create an empty arraylist object 'states'
List<String> states = new ArrayList<String>();
// add state in the arraylist, Florida multiple times
states.add("California");
states.add("Texas");
states.add("Montana");
states.add("Arizona");
states.add("Florida");
states.add("Michigan");
states.add("New Jersey");
states.add("Washington");
states.add("Ohio");
states.add("Minnesota");
states.add("Colorado");
states.add("Missouri");
states.add("Nevada");
// create another ArrayList for items to be removed
List<String> statesToBeRemoved = new ArrayList<String>();
statesToBeRemoved.add("Minnesota");
statesToBeRemoved.add("Missouri");
statesToBeRemoved.add("Montana");
statesToBeRemoved.add("Michigan");
System.out.println("The states list before the removeAll() method call: \n" + states);
// removes all elements specified in the ArrayList statesToBeRemoved
states.removeAll(statesToBeRemoved);
System.out.println("\nThe states list after the removeAll() method call: \n" + states);
}
}