Skip to content

Commit a470906

Browse files
Merge pull request eugenp#5454 from danielguedesb/master
BAEL-2242
2 parents 99d309b + 6b3803a commit a470906

5 files changed

Lines changed: 175 additions & 0 deletions

File tree

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.removal;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
6+
public class CollectionRemoveIf {
7+
8+
public static void main(String args[]) {
9+
Collection<String> names = new ArrayList<>();
10+
names.add("John");
11+
names.add("Ana");
12+
names.add("Mary");
13+
names.add("Anthony");
14+
names.add("Mark");
15+
16+
names.removeIf(e -> e.startsWith("A"));
17+
System.out.println(String.join(",", names));
18+
}
19+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.removal;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.Iterator;
6+
7+
public class Iterators {
8+
9+
public static void main(String args[]) {
10+
Collection<String> names = new ArrayList<>();
11+
names.add("John");
12+
names.add("Ana");
13+
names.add("Mary");
14+
names.add("Anthony");
15+
names.add("Mark");
16+
17+
Iterator<String> i = names.iterator();
18+
19+
while (i.hasNext()) {
20+
String e = i.next();
21+
if (e.startsWith("A")) {
22+
i.remove();
23+
}
24+
}
25+
26+
System.out.println(String.join(",", names));
27+
}
28+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.removal;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.stream.Collectors;
6+
7+
public class StreamFilterAndCollector {
8+
9+
public static void main(String args[]) {
10+
Collection<String> names = new ArrayList<>();
11+
names.add("John");
12+
names.add("Ana");
13+
names.add("Mary");
14+
names.add("Anthony");
15+
names.add("Mark");
16+
17+
Collection<String> filteredCollection = names
18+
.stream()
19+
.filter(e -> !e.startsWith("A"))
20+
.collect(Collectors.toList());
21+
System.out.println(String.join(",", filteredCollection));
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.removal;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.List;
6+
import java.util.Map;
7+
import java.util.stream.Collectors;
8+
9+
public class StreamPartitioningBy {
10+
11+
public static void main(String args[]) {
12+
Collection<String> names = new ArrayList<>();
13+
names.add("John");
14+
names.add("Ana");
15+
names.add("Mary");
16+
names.add("Anthony");
17+
names.add("Mark");
18+
19+
Map<Boolean, List<String>> classifiedElements = names
20+
.stream()
21+
.collect(Collectors.partitioningBy((String e) -> !e.startsWith("A")));
22+
23+
String matching = String.join(",", classifiedElements.get(Boolean.TRUE));
24+
String nonMatching = String.join(",", classifiedElements.get(Boolean.FALSE));
25+
System.out.println("Matching elements: " + matching);
26+
System.out.println("Non matching elements: " + nonMatching);
27+
}
28+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.baeldung.removal;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import java.util.*;
7+
import java.util.stream.Collectors;
8+
9+
import static org.hamcrest.Matchers.is;
10+
import static org.junit.Assert.assertThat;
11+
12+
public class RemovalUnitTest {
13+
14+
Collection<String> names;
15+
Collection<String> expected;
16+
Collection<String> removed;
17+
18+
@Before
19+
public void setupTestData() {
20+
names = new ArrayList<>();
21+
expected = new ArrayList<>();
22+
removed = new ArrayList<>();
23+
24+
names.add("John");
25+
names.add("Ana");
26+
names.add("Mary");
27+
names.add("Anthony");
28+
names.add("Mark");
29+
30+
expected.add("John");
31+
expected.add("Mary");
32+
expected.add("Mark");
33+
34+
removed.add("Ana");
35+
removed.add("Anthony");
36+
}
37+
38+
@Test
39+
public void givenCollectionOfNames_whenUsingIteratorToRemoveAllNamesStartingWithLetterA_finalListShouldContainNoNamesStartingWithLetterA() {
40+
Iterator<String> i = names.iterator();
41+
42+
while (i.hasNext()) {
43+
String e = i.next();
44+
if (e.startsWith("A")) {
45+
i.remove();
46+
}
47+
}
48+
49+
assertThat(names, is(expected));
50+
}
51+
52+
@Test
53+
public void givenCollectionOfNames_whenUsingRemoveIfToRemoveAllNamesStartingWithLetterA_finalListShouldContainNoNamesStartingWithLetterA() {
54+
names.removeIf(e -> e.startsWith("A"));
55+
assertThat(names, is(expected));
56+
}
57+
58+
@Test
59+
public void givenCollectionOfNames_whenUsingStreamToFilterAllNamesStartingWithLetterA_finalListShouldContainNoNamesStartingWithLetterA() {
60+
Collection<String> filteredCollection = names
61+
.stream()
62+
.filter(e -> !e.startsWith("A"))
63+
.collect(Collectors.toList());
64+
assertThat(filteredCollection, is(expected));
65+
}
66+
67+
@Test
68+
public void givenCollectionOfNames_whenUsingStreamAndPartitioningByToFindNamesThatStartWithLetterA_shouldFind3MatchingAnd2NonMatching() {
69+
Map<Boolean, List<String>> classifiedElements = names
70+
.stream()
71+
.collect(Collectors.partitioningBy((String e) -> !e.startsWith("A")));
72+
73+
assertThat(classifiedElements.get(Boolean.TRUE), is(expected));
74+
assertThat(classifiedElements.get(Boolean.FALSE), is(removed));
75+
}
76+
77+
}

0 commit comments

Comments
 (0)