如何从一个 List 中删除 null 元素

This commit is contained in:
YuCheng Hu 2022-04-30 18:16:31 -04:00
parent 669e2be78c
commit 8c5ab530f0
1 changed files with 12 additions and 6 deletions

View File

@ -1,9 +1,9 @@
package com.ossez.collections; package com.ossez.collections;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder; import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsInRelativeOrder; import static org.hamcrest.Matchers.containsInRelativeOrder;
import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.hasSize;
import static org.junit.Assert.assertThat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
@ -13,22 +13,28 @@ import java.util.List;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.PredicateUtils; import org.apache.commons.collections4.PredicateUtils;
import org.junit.Test;
import com.google.common.base.Predicates; import com.google.common.base.Predicates;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.collect.Sets; import com.google.common.collect.Sets;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
/**
* Test for list Clean up
*
* <p><a href="https://www.ossez.com/t/java-list-null/13940">https://www.ossez.com/t/java-list-null/13940</a></p>
*
* @author YuCheng
*/
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class JavaCollectionCleanupUnitTest { public class JavaCollectionCleanupUnitTest {
// tests - removing nulls
@Test @Test
public void givenListContainsNulls_whenRemovingNullsWithPlainJava_thenCorrect() { public void givenListContainsNulls_whenRemovingNullsWithPlainJava_thenCorrect() {
final List<Integer> list = Lists.newArrayList(null, 1, null); final List<Integer> list = Lists.newArrayList(null, 1, null);
while (list.remove(null)) while (list.remove(null));
;
assertThat(list, hasSize(1)); assertThat(list, hasSize(1));
} }