forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
eugenp
committed
Jun 10, 2014
1 parent
bb662b4
commit 9c3ff77
Showing
2 changed files
with
94 additions
and
17 deletions.
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
core-java/src/test/java/org/baeldung/java/collections/JavaCollectionCleanupUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.baeldung.java.collections; | ||
|
||
import static org.hamcrest.Matchers.hasSize; | ||
import static org.junit.Assert.assertThat; | ||
|
||
import java.util.List; | ||
|
||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.apache.commons.collections4.PredicateUtils; | ||
import org.junit.Test; | ||
|
||
import com.google.common.base.Predicates; | ||
import com.google.common.collect.Iterables; | ||
import com.google.common.collect.Lists; | ||
|
||
public class JavaCollectionCleanupUnitTest { | ||
|
||
// removing nulls | ||
|
||
@Test | ||
public final void givenListContainsNulls_whenRemovingNullsWithPlainJava_thenCorrect() { | ||
final List<Integer> list = Lists.newArrayList(null, 1, null); | ||
while (list.remove(null)) | ||
; | ||
|
||
assertThat(list, hasSize(1)); | ||
} | ||
|
||
@Test | ||
public final void givenListContainsNulls_whenRemovingNullsWithGuavaV1_thenCorrect() { | ||
final List<Integer> listWithNulls = Lists.newArrayList(null, 1, null); | ||
Iterables.removeIf(listWithNulls, Predicates.isNull()); | ||
|
||
assertThat(listWithNulls, hasSize(1)); | ||
} | ||
|
||
@Test | ||
public final void givenListContainsNulls_whenRemovingNullsWithGuavaV2_thenCorrect() { | ||
final List<Integer> listWithNulls = Lists.newArrayList(null, 1, null, 2, 3); | ||
final List<Integer> listWithoutNulls = Lists.newArrayList(Iterables.filter(listWithNulls, Predicates.notNull())); | ||
|
||
assertThat(listWithoutNulls, hasSize(3)); | ||
} | ||
|
||
@Test | ||
public final void givenListContainsNulls_whenRemovingNullsWithCommonsCollections_thenCorrect() { | ||
final List<Integer> list = Lists.newArrayList(null, 1, 2, null, 3, null); | ||
CollectionUtils.filter(list, PredicateUtils.notNullPredicate()); | ||
|
||
assertThat(list, hasSize(3)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters