Skip to content

Commit b5ad313

Browse files
committed
Add blopost example for the removeIf, removeAll and isEmpty method of the arrayList in java
1 parent 3f27168 commit b5ad313

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
/**
5+
* An example java program for an isEmpty method
6+
* of the ArrayList class in java.
7+
*
8+
* @author Gaurav Kukade at coderolls
9+
*/
10+
public class ArrayListIsEmptyExample {
11+
12+
public static void main(String[] args) {
13+
14+
// create an arraylyst teams
15+
ArrayList<String> teams = new ArrayList<String>();
16+
17+
// add elements to the arraylist teams
18+
teams.add("LA Galaxy");
19+
teams.add("Orlando City FC");
20+
teams.add("Portland Timbers");
21+
teams.add("Columbus Crew");
22+
23+
boolean isTeamsEmpty = teams.isEmpty();
24+
25+
if(isTeamsEmpty) {
26+
System.out.println("Teams arraylist is empty.");
27+
}
28+
else {
29+
System.out.println("Teams arraylist is not empty.");
30+
}
31+
32+
// create an arraylist players
33+
ArrayList<String> players = new ArrayList<String>();
34+
35+
boolean isPlayersEmpty = players.isEmpty();
36+
37+
if(isPlayersEmpty) {
38+
System.out.println("Players arraylist is empty.");
39+
}
40+
else {
41+
System.out.println("Players arraylist is not empty.");
42+
}
43+
}
44+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.function.Predicate;
6+
7+
/**
8+
* A java program showing an example for the removeAll()
9+
* method of the ArrayList class in Java.
10+
*
11+
* @author gaurav
12+
*/
13+
public class ArrayListRemoveAllExample {
14+
15+
public static void main(String[] args) {
16+
17+
// create an empty arraylist object 'states'
18+
List<String> states = new ArrayList<String>();
19+
20+
// add state in the arraylist, Florida multiple times
21+
states.add("California");
22+
states.add("Texas");
23+
states.add("Montana");
24+
states.add("Arizona");
25+
states.add("Florida");
26+
states.add("Michigan");
27+
states.add("New Jersey");
28+
states.add("Washington");
29+
states.add("Ohio");
30+
states.add("Minnesota");
31+
states.add("Colorado");
32+
states.add("Missouri");
33+
states.add("Nevada");
34+
35+
// create another ArrayList for items to be removed
36+
List<String> statesToBeRemoved = new ArrayList<String>();
37+
statesToBeRemoved.add("Minnesota");
38+
statesToBeRemoved.add("Missouri");
39+
statesToBeRemoved.add("Montana");
40+
statesToBeRemoved.add("Michigan");
41+
42+
System.out.println("The states list before the removeAll() method call: \n" + states);
43+
44+
// removes all elements specified in the ArrayList statesToBeRemoved
45+
states.removeAll(statesToBeRemoved);
46+
47+
System.out.println("\nThe states list after the removeAll() method call: \n" + states);
48+
}
49+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.function.Predicate;
6+
7+
/**
8+
* A java program showing a NullPointerException case
9+
* for the removeAll() method of the ArrayList class in Java.
10+
*
11+
* @author gaurav
12+
*/
13+
public class ArrayListRemoveAllNullPointerException {
14+
15+
public static void main(String[] args) {
16+
17+
// create an empty arraylist object 'states'
18+
List<String> states = new ArrayList<String>();
19+
20+
// add state in the arraylist, Florida multiple times
21+
states.add("California");
22+
states.add("Texas");
23+
states.add("Montana");
24+
states.add("Arizona");
25+
states.add("Florida");
26+
states.add("Michigan");
27+
states.add("New Jersey");
28+
states.add("Washington");
29+
states.add("Ohio");
30+
states.add("Minnesota");
31+
states.add("Colorado");
32+
states.add("Missouri");
33+
states.add("Nevada");
34+
35+
// create another ArrayList with null value,
36+
//so that removeAll will throw NullPointerException
37+
List<String> statesToBeRemoved =null;
38+
39+
System.out.println("The states list before the removeAll() method call: \n" + states);
40+
41+
// removes all elements specified in the ArrayList statesToBeRemoved
42+
states.removeAll(statesToBeRemoved);
43+
44+
System.out.println("\nThe states list after the removeAll() method call: \n" + states);
45+
}
46+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.gaurav.ExProject.ArrayList;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
import java.util.function.Predicate;
6+
7+
/**
8+
* A java program showing an example for the removeIf()
9+
* method of the ArrayList class in Java.
10+
*
11+
* @author gaurav
12+
*/
13+
public class ArrayListRemoveIfExample {
14+
15+
public static void main(String[] args) {
16+
17+
// create an empty arraylist object 'states'
18+
List<String> states = new ArrayList<String>();
19+
20+
// add state in the arraylist, Florida multiple times
21+
states.add("California");
22+
states.add("Texas");
23+
states.add("Florida");
24+
states.add("Florida");
25+
states.add("New Jersey");
26+
states.add("Washington");
27+
states.add("Florida");
28+
29+
System.out.println("The states list before the removeIf() call: \n" + states);
30+
31+
// a predicate for the condition
32+
Predicate p = s -> s.equals("Florida");
33+
states.removeIf(p); // removes all elements which satisfy the predicate p
34+
35+
System.out.println("\nThe states list after the removeIf() call: \n" + states);
36+
}
37+
}

0 commit comments

Comments
 (0)