BAEL-2242
This commit is contained in:
parent
194a6541c4
commit
6b3803a683
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.removal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
|
||||
public class CollectionRemoveIf {
|
||||
|
||||
public static void main(String args[]) {
|
||||
Collection<String> names = new ArrayList<>();
|
||||
names.add("John");
|
||||
names.add("Ana");
|
||||
names.add("Mary");
|
||||
names.add("Anthony");
|
||||
names.add("Mark");
|
||||
|
||||
names.removeIf(e -> e.startsWith("A"));
|
||||
System.out.println(String.join(",", names));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.removal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class Iterators {
|
||||
|
||||
public static void main(String args[]) {
|
||||
Collection<String> names = new ArrayList<>();
|
||||
names.add("John");
|
||||
names.add("Ana");
|
||||
names.add("Mary");
|
||||
names.add("Anthony");
|
||||
names.add("Mark");
|
||||
|
||||
Iterator<String> i = names.iterator();
|
||||
|
||||
while (i.hasNext()) {
|
||||
String e = i.next();
|
||||
if (e.startsWith("A")) {
|
||||
i.remove();
|
||||
}
|
||||
}
|
||||
|
||||
System.out.println(String.join(",", names));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
package com.baeldung.removal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class StreamFilterAndCollector {
|
||||
|
||||
public static void main(String args[]) {
|
||||
Collection<String> names = new ArrayList<>();
|
||||
names.add("John");
|
||||
names.add("Ana");
|
||||
names.add("Mary");
|
||||
names.add("Anthony");
|
||||
names.add("Mark");
|
||||
|
||||
Collection<String> filteredCollection = names
|
||||
.stream()
|
||||
.filter(e -> !e.startsWith("A"))
|
||||
.collect(Collectors.toList());
|
||||
System.out.println(String.join(",", filteredCollection));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.removal;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class StreamPartitioningBy {
|
||||
|
||||
public static void main(String args[]) {
|
||||
Collection<String> names = new ArrayList<>();
|
||||
names.add("John");
|
||||
names.add("Ana");
|
||||
names.add("Mary");
|
||||
names.add("Anthony");
|
||||
names.add("Mark");
|
||||
|
||||
Map<Boolean, List<String>> classifiedElements = names
|
||||
.stream()
|
||||
.collect(Collectors.partitioningBy((String e) -> !e.startsWith("A")));
|
||||
|
||||
String matching = String.join(",", classifiedElements.get(Boolean.TRUE));
|
||||
String nonMatching = String.join(",", classifiedElements.get(Boolean.FALSE));
|
||||
System.out.println("Matching elements: " + matching);
|
||||
System.out.println("Non matching elements: " + nonMatching);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,77 @@
|
|||
package com.baeldung.removal;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
public class RemovalUnitTest {
|
||||
|
||||
Collection<String> names;
|
||||
Collection<String> expected;
|
||||
Collection<String> removed;
|
||||
|
||||
@Before
|
||||
public void setupTestData() {
|
||||
names = new ArrayList<>();
|
||||
expected = new ArrayList<>();
|
||||
removed = new ArrayList<>();
|
||||
|
||||
names.add("John");
|
||||
names.add("Ana");
|
||||
names.add("Mary");
|
||||
names.add("Anthony");
|
||||
names.add("Mark");
|
||||
|
||||
expected.add("John");
|
||||
expected.add("Mary");
|
||||
expected.add("Mark");
|
||||
|
||||
removed.add("Ana");
|
||||
removed.add("Anthony");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCollectionOfNames_whenUsingIteratorToRemoveAllNamesStartingWithLetterA_finalListShouldContainNoNamesStartingWithLetterA() {
|
||||
Iterator<String> i = names.iterator();
|
||||
|
||||
while (i.hasNext()) {
|
||||
String e = i.next();
|
||||
if (e.startsWith("A")) {
|
||||
i.remove();
|
||||
}
|
||||
}
|
||||
|
||||
assertThat(names, is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCollectionOfNames_whenUsingRemoveIfToRemoveAllNamesStartingWithLetterA_finalListShouldContainNoNamesStartingWithLetterA() {
|
||||
names.removeIf(e -> e.startsWith("A"));
|
||||
assertThat(names, is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCollectionOfNames_whenUsingStreamToFilterAllNamesStartingWithLetterA_finalListShouldContainNoNamesStartingWithLetterA() {
|
||||
Collection<String> filteredCollection = names
|
||||
.stream()
|
||||
.filter(e -> !e.startsWith("A"))
|
||||
.collect(Collectors.toList());
|
||||
assertThat(filteredCollection, is(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCollectionOfNames_whenUsingStreamAndPartitioningByToFindNamesThatStartWithLetterA_shouldFind3MatchingAnd2NonMatching() {
|
||||
Map<Boolean, List<String>> classifiedElements = names
|
||||
.stream()
|
||||
.collect(Collectors.partitioningBy((String e) -> !e.startsWith("A")));
|
||||
|
||||
assertThat(classifiedElements.get(Boolean.TRUE), is(expected));
|
||||
assertThat(classifiedElements.get(Boolean.FALSE), is(removed));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue