Skip to content

Commit f32fd57

Browse files
authored
Merge pull request #17026 from sk1418/impr-rm-el-list
[impr-rm-el-list] improvement remove the last element from a list
2 parents 34e46a5 + aa1bc86 commit f32fd57

File tree

4 files changed

+87
-44
lines changed

4 files changed

+87
-44
lines changed

core-java-modules/core-java-collections-array-list/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@
4545
</build>
4646

4747
<properties>
48-
<maven-compiler-plugin.source>16</maven-compiler-plugin.source>
49-
<maven-compiler-plugin.target>16</maven-compiler-plugin.target>
48+
<maven-compiler-plugin.source>21</maven-compiler-plugin.source>
49+
<maven-compiler-plugin.target>21</maven-compiler-plugin.target>
5050
<surefire.plugin.version>3.0.0-M3</surefire.plugin.version>
5151
</properties>
5252

53-
</project>
53+
</project>

core-java-modules/core-java-collections-array-list/src/main/java/com/baeldung/java/list/RemoveFromList.java

Lines changed: 0 additions & 40 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.baeldung.list.removeelement;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.Iterator;
7+
import java.util.List;
8+
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
public class RemoveElementFromAnArrayListUnitTest {
12+
private List<String> getSportList() {
13+
List<String> sports = new ArrayList<>();
14+
sports.add("Football");
15+
sports.add("Basketball");
16+
sports.add("Baseball");
17+
sports.add("Boxing");
18+
sports.add("Cycling");
19+
return sports;
20+
}
21+
22+
@Test
23+
void whenRemoveByIndex_thenCorrect() {
24+
List<String> sports = getSportList();
25+
assertThat(sports).hasSize(5).contains("Basketball");
26+
27+
sports.remove(1); //removing Basketball
28+
assertThat(sports).hasSize(4).doesNotContain("Basketball");
29+
}
30+
31+
@Test
32+
void whenRemoveLastByIndex_thenCorrect() {
33+
List<String> sports = getSportList();
34+
List<String> expected = List.of("Football", "Basketball", "Baseball", "Boxing");
35+
36+
sports.remove(sports.size() - 1);
37+
assertThat(sports).isEqualTo(expected);
38+
}
39+
40+
@Test
41+
void whenUsingJDK21RemoveLast_thenCorrect() {
42+
List<String> sports = getSportList();
43+
List<String> expected = List.of("Football", "Basketball", "Baseball", "Boxing");
44+
45+
sports.removeLast();
46+
assertThat(sports).isEqualTo(expected);
47+
}
48+
49+
50+
@Test
51+
void whenRemoveByElement_thenCorrect() {
52+
List<String> sports = getSportList();
53+
assertThat(sports).hasSize(5).contains("Basketball");
54+
55+
sports.remove("Basketball");
56+
assertThat(sports).hasSize(4).doesNotContain("Basketball");
57+
}
58+
59+
@Test
60+
void whenRemovingWhileIterating_thenCorrect() {
61+
List<String> sports = getSportList();
62+
assertThat(sports).hasSize(5).contains("Basketball");
63+
64+
Iterator<String> iterator = sports.iterator();
65+
while (iterator.hasNext()) {
66+
if (iterator.next().equals("Basketball")) {
67+
iterator.remove();
68+
break;
69+
}
70+
}
71+
assertThat(sports).hasSize(4).doesNotContain("Basketball");
72+
}
73+
74+
@Test
75+
void whenRemoveIf_thenCorrect() {
76+
List<String> sports = getSportList();
77+
assertThat(sports).hasSize(5).contains("Basketball");
78+
79+
sports.removeIf(p -> p.equals("Basketball"));
80+
81+
assertThat(sports).hasSize(4).doesNotContain("Basketball");
82+
}
83+
}

core-java-modules/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<module>core-java-12</module>
4848
<module>core-java-13</module>
4949
<module>core-java-15</module>
50-
<module>core-java-collections-array-list</module>
50+
<!--<module>core-java-collections-array-list</module> --> <!-- requires JDK 21 -->
5151
<module>core-java-collections-array-list-2</module>
5252
<module>core-java-collections-list-4</module>
5353
<module>core-java-collections-list-5</module>

0 commit comments

Comments
 (0)