Added stream removal from collection (#1131)
This commit is contained in:
parent
a9d8c5f3a3
commit
5c8acbac57
|
@ -3,9 +3,12 @@ package com.baeldung.java.concurrentmodification;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
import java.util.ConcurrentModificationException;
|
import java.util.ConcurrentModificationException;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static java.util.stream.Collectors.toList;
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.assertj.core.util.Lists.newArrayList;
|
import static org.assertj.core.util.Lists.newArrayList;
|
||||||
|
|
||||||
|
@ -13,7 +16,7 @@ public class ConcurrentModificationUnitTest {
|
||||||
@Test(expected = ConcurrentModificationException.class)
|
@Test(expected = ConcurrentModificationException.class)
|
||||||
public void givenIterating_whenRemoving_thenThrowException() throws InterruptedException {
|
public void givenIterating_whenRemoving_thenThrowException() throws InterruptedException {
|
||||||
|
|
||||||
ArrayList<Integer> integers = newArrayList(1, 2, 3);
|
List<Integer> integers = newArrayList(1, 2, 3);
|
||||||
|
|
||||||
for (Integer integer : integers) {
|
for (Integer integer : integers) {
|
||||||
integers.remove(1);
|
integers.remove(1);
|
||||||
|
@ -23,7 +26,7 @@ public class ConcurrentModificationUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenIterating_whenUsingIteratorRemove_thenNoError() throws InterruptedException {
|
public void givenIterating_whenUsingIteratorRemove_thenNoError() throws InterruptedException {
|
||||||
|
|
||||||
ArrayList<Integer> integers = newArrayList(1, 2, 3);
|
List<Integer> integers = newArrayList(1, 2, 3);
|
||||||
|
|
||||||
for (Iterator<Integer> iterator = integers.iterator(); iterator.hasNext();) {
|
for (Iterator<Integer> iterator = integers.iterator(); iterator.hasNext();) {
|
||||||
Integer integer = iterator.next();
|
Integer integer = iterator.next();
|
||||||
|
@ -38,8 +41,8 @@ public class ConcurrentModificationUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void givenIterating_whenUsingRemovalList_thenNoError() throws InterruptedException {
|
public void givenIterating_whenUsingRemovalList_thenNoError() throws InterruptedException {
|
||||||
|
|
||||||
ArrayList<Integer> integers = newArrayList(1, 2, 3);
|
List<Integer> integers = newArrayList(1, 2, 3);
|
||||||
ArrayList<Integer> toRemove = newArrayList();
|
List<Integer> toRemove = newArrayList();
|
||||||
|
|
||||||
for (Integer integer : integers) {
|
for (Integer integer : integers) {
|
||||||
if(integer == 2) {
|
if(integer == 2) {
|
||||||
|
@ -54,10 +57,24 @@ public class ConcurrentModificationUnitTest {
|
||||||
@Test
|
@Test
|
||||||
public void whenUsingRemoveIf_thenRemoveElements() throws InterruptedException {
|
public void whenUsingRemoveIf_thenRemoveElements() throws InterruptedException {
|
||||||
|
|
||||||
ArrayList<Integer> integers = newArrayList(1, 2, 3);
|
Collection<Integer> integers = newArrayList(1, 2, 3);
|
||||||
|
|
||||||
integers.removeIf(i -> i == 2);
|
integers.removeIf(i -> i == 2);
|
||||||
|
|
||||||
assertThat(integers).containsExactly(1, 3);
|
assertThat(integers).containsExactly(1, 3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void whenUsingStream_thenRemoveElements() {
|
||||||
|
Collection<Integer> integers = newArrayList(1, 2, 3);
|
||||||
|
|
||||||
|
List<String> collected = integers
|
||||||
|
.stream()
|
||||||
|
.filter(i -> i != 2)
|
||||||
|
.map(Object::toString)
|
||||||
|
.collect(toList());
|
||||||
|
|
||||||
|
assertThat(collected).containsExactly("1", "3");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue