[BAEL-2032] Operate on an item in a Stream then remove it (#5086)

* [BAEL-2032] Operate on an item in a Stream then remove it

* [BAEL-2032] Refactored unit test

* [BAEL-2032] Added a new test for filter and used logger

* Stored the predicate in a separate variable
This commit is contained in:
Siben Nayak 2018-08-27 17:37:35 +05:30 committed by pauljervis
parent 1c78267e4b
commit ae287cb55f
1 changed files with 4 additions and 2 deletions

View File

@ -2,6 +2,7 @@ package com.baeldung.collection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.junit.Assert; import org.junit.Assert;
@ -35,8 +36,9 @@ public class StreamOperateAndRemoveUnitTest {
@Test @Test
public void givenAListOf10Items_whenOperateAndRemoveQualifiedItemsUsingRemoveIf_thenListContains5Items() { public void givenAListOf10Items_whenOperateAndRemoveQualifiedItemsUsingRemoveIf_thenListContains5Items() {
itemList.stream().filter(item -> item.isQualified()).forEach(item -> item.operate()); final Predicate<Item> isQualified = item -> item.isQualified();
itemList.removeIf(item -> item.isQualified()); itemList.stream().filter(isQualified).forEach(item -> item.operate());
itemList.removeIf(isQualified);
Assert.assertEquals(5, itemList.size()); Assert.assertEquals(5, itemList.size());
} }