1 Introduction
Evil null
has plagued programmers for a long time, and also brought many find it difficult to cause serious damage NullPointerException
. We need to avoid it as much as possible, there is a simple way is to deal with before it enters the next round, we'll strangle it in the cradle.
This article describes the four types of methods, namely List
the method interface Stream
, Guava
and Apache Commons Collections
to delete an List
inside null
value. I hope readers can learn by analogy, to get more inspiration.
2 four categories Methods
2.1 List comes methods
List
There are many remove
methods you can use, we are able to meet the following three requirements:
List.remove(Object o)
: Delete an element, success is returnedtrue
; it only deletes a note;List.removeAll(Collection<?> c)
: Delete all cases there is a collection of c, pay attention to the Senate is not an element;List.removeIf(Predicate<? super E> filter)
: Delete all elements to meet the conditions, to the Senate for the Lambda expressions.
code show as below:
@Test
public void listRemove() {
List<String> list = Lists.newArrayList("Cup", null, "Apple", null, "Desk");
List<String> expected = Lists.newArrayList("Cup", "Apple", "Desk");
//remove
while (list.remove(null));//巧妙利用循环删除
assertEquals(expected, list);
//removeAll
list = Lists.newArrayList("Cup", null, "Apple", null, "Desk");
list.removeAll(Collections.singletonList(null));
assertEquals(expected, list);
//removeIf
list = Lists.newArrayList("Cup", null, "Apple", null, "Desk");
list.removeIf(Objects::isNull);
assertEquals(expected, list);
}
2.2 Stream way
Stream
The method is easily understood, is to add a filter, the filter is non-empty condition, the specific code is as follows:
@Test
public void stream() {
List<String> list = Lists.newArrayList("Cup", null, "Apple", null, "Desk");
List<String> expected = Lists.newArrayList("Cup", "Apple", "Desk");
List<String> result = list.parallelStream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
assertEquals(expected, result);
}
2.3 Guava library
Guava
Is a very good Java library that provides a lot of good way to deal with a set of classes, this time using a Iterables
class processing code is as follows:
@Test
public void guava() {
//改变原有List
List<String> list = Lists.newArrayList("Cup", null, "Apple", null, "Desk");
List<String> expected = Lists.newArrayList("Cup", "Apple", "Desk");
Iterables.removeIf(list, Objects::isNull);
assertEquals(expected, list);
//保留原有List
list = Lists.newArrayList("Cup", null, "Apple", null, "Desk");
List<String> result = Lists.newArrayList(Iterables.filter(list, Objects::nonNull));
assertEquals(expected, result);
}
This article provides two methods, one can change the original List
, and the other does not.
2.4 Apache Commons library
Apache Commons Collections
Also provides a very convenient method, the specific code is as follows:
@Test
public void apacheCommonsCollections() {
List<String> list = Lists.newArrayList("Cup", null, "Apple", null, "Desk");
List<String> expected = Lists.newArrayList("Cup", "Apple", "Desk");
CollectionUtils.filter(list, Objects::nonNull);
assertEquals(expected, list);
}
3 summary
Although this article talking about the List
deletion null
process, but slightly modified, can be extended to collections remove the value of certain conditions, it is still very useful.
Welcome to public concern number < pumpkin slow, said >, you will continue to update ...
More books, more sharing; and more writing, more than finishing.