列表 是一个有序的对象集合,允许存储重复的值或 无效的 值,按插入顺序。因此,在许多情况下删除空值非常重要。
null
例如:
Input: [Geeks, null, forGeeks, null, A computer portal] Output: [Geeks, forGeeks, A computer portal] Input: [1, null, 2, 3, null, 4] Output: [1, 2, 3, 4]
以下是在Java中从列表中删除空值的方法:
- 使用列表。删除() 列表界面提供了一个预定义的方法 移除(元素) 用于从列表中删除所传递元素的单个匹配项(如果找到)。
算法 :
- 获取具有空值的列表。
- 反复打电话 删除(空) 直到删除所有空值。
- 返回/打印列表(现在删除所有空值)。
// Java Program to remove nulls
// from a List using List.remove()
import
java.util.*;
class
GFG {
public
static
void
main(String[] args)
{
// Create the list with null values
List<String> list =
new
ArrayList<>(
Arrays.asList(
"Geeks"
,
null
,
"forGeeks"
,
null
,
"A computer portal"
));
// Print the list
System.out.println(
"Initial List: "
+ list);
// Removing nulls using List.remove()
// Repeatedly call remove() till all null are removed
while
(list.remove(
null
)) {
}
// Print the list
System.out.println(
"Modified List: "
+ list);
}
}
输出:Initial List: [Geeks, null, forGeeks, null, A computer portal] Modified List: [Geeks, forGeeks, A computer portal]
- 使用列表。removeAll() :List接口提供了另一个预定义的方法 removeAll(收藏) 用于从列表中删除已传递集合的所有元素(如果找到)。
算法 :
- 获取具有空值的列表。
- 使用 收藏。单音列表(空)
- 呼叫 removeAll(收藏) 名单上只有一次。
- 返回/打印列表(现在删除所有空值)。
// Java Program to remove nulls
// from a List using List.removeAll()
import
java.util.*;
class
GFG {
public
static
void
main(String[] args)
{
// Create the list with null values
List<String> list =
new
ArrayList<>(
Arrays.asList(
"Geeks"
,
null
,
"forGeeks"
,
null
,
"A computer portal"
));
// Print the list
System.out.println(
"Initial List: "
+ list);
// Removing nulls using List.removeAll()
// passing a collection with single element "null"
list.removeAll(Collections.singletonList(
null
));
// Print the list
System.out.println(
"Modified List: "
+ list);
}
}
输出:Initial List: [Geeks, null, forGeeks, null, A computer portal] Modified List: [Geeks, forGeeks, A computer portal]
- 使用迭代器 : 迭代器 是一个属于集合框架的接口。它允许用户遍历集合、访问数据元素并删除集合中的数据元素。
算法 :
- 获取具有空值的列表。
- 从列表中创建迭代器
- 使用创建的迭代器遍历列表中的每个元素
- 检查每个元素是否为空。如果发现空值,请调用 迭代元素。删除() 关于那一点。
- 返回/打印列表(现在删除所有空值)。
节目:
// Java Program to remove nulls
// from a List using iterator
import
java.util.*;
class
GFG {
// Generic function to remove Null Using Iterator
public
static
<T> List<T> removeNullUsingIterator(List<T> list)
{
// Create an iterator from the list
Iterator<T> itr = list.iterator();
// Find and remove all null
while
(itr.hasNext()) {
if
(itr.next() ==
null
)
itr.remove();
// remove nulls
}
// Return the null
return
list;
}
public
static
void
main(String[] args)
{
// Create the list with null values
List<String> list =
new
ArrayList<>(
Arrays.asList(
"Geeks"
,
null
,
"forGeeks"
,
null
,
"A computer portal"
));
// Print the list
System.out.println(
"Initial List: "
+ list);
// Removing nulls using iterator
list = removeNullUsingIterator(list);
// Print the list
System.out.println(
"Modified List: "
+ list);
}
}
输出:Initial List: [Geeks, null, forGeeks, null, A computer portal] Modified List: [Geeks, forGeeks, A computer portal]
- 使用番石榴 :Guava Iterables课程提供 无法忍受。removeIf(Iterable,谓词) 从指定的Iterable(或实现Iterable的集合)中移除满足所提供 谓语 .
算法 :
- 获取具有空值的列表。
- 获取谓词条件 谓词。isNull() 传入removeIf()的参数
- 呼叫 无法忍受。removeIf(列表,谓词) 其中,List是具有空值的原始列表,谓词是谓词。isNull()实例。
- 返回/打印列表(现在删除所有空值)。
// Java Program to remove nulls
// from a List using Guava Iterables
import
com.google.common.base.Predicates;
import
com.google.common.collect.Iterables;
import
java.util.*;
class
GFG {
public
static
void
main(String[] args)
{
// Create the list with null values
List<String> list =
new
ArrayList<>(
Arrays.asList(
"Geeks"
,
null
,
"forGeeks"
,
null
,
"A computer portal"
));
// Print the list
System.out.println(
"Initial List: "
+ list);
// Removing nulls using Guava Iterables
// using Predicate condition isNull()
Iterables.removeIf(list, Predicates.isNull());
// Print the list
System.out.println(
"Modified List: "
+ list);
}
}
输出:Initial List: [Geeks, null, forGeeks, null, A computer portal] Modified List: [Geeks, forGeeks, A computer portal]
- 使用Apache Commons Collections筛选器() :Apache Commons Collections CollectionUtils类提供 过滤器(Iterable,谓词) 从指定的iterable中删除不满足所提供的 谓语 .
算法 :
- 获取具有空值的列表。
- 获取谓词条件 谓词EUTILS。notNullPredicate() 传入filter()的参数,使传递条件的元素 NotNull 保留在列表中,而所有其他内容都会被过滤。
- 呼叫 收藏品。过滤器(列表,PredicateUtils.notNullPredicate()) 其中,List是具有空值的原始列表,谓词是PredicateUtils。notNullPredicate()实例。
- 返回/打印列表(现在删除所有空值)。
节目:
// Java Program to remove nulls
// from a List using Apache Common COllection Filter()
import
org.apache.commons.collections4.CollectionUtils;
import
org.apache.commons.collections4.PredicateUtils;
import
java.util.*;
class
GFG {
public
static
void
main(String[] args)
{
// Create the list with null values
List<String> list =
new
ArrayList<>(
Arrays.asList(
"Geeks"
,
null
,
"forGeeks"
,
null
,
"A computer portal"
));
// Print the list
System.out.println(
"Initial List: "
+ list);
// Removing nulls using Apache Common filter()
// using Predicate condition notNullPredicate()
CollectionUtils.filter(list, PredicateUtils.notNullPredicate());
// Print the list
System.out.println(
"Modified List: "
+ list);
}
}
输出:Initial List: [Geeks, null, forGeeks, null, A computer portal] Modified List: [Geeks, forGeeks, A computer portal]
- 使用Lambdas(Java8) : 流动过滤器() 方法可以在Java8中使用,该方法返回由元素组成的流 匹配给定谓词条件的。
算法 :
- 获取具有空值的列表。
- 使用以下命令从列表中创建流: 列表流()
- 使用 列表过滤器(x->x!=null)
- 使用 .collect(Collectors.toList()
- 返回/打印列表(现在删除所有空值)。
// Java Program to remove nulls
// from a List using Apache Common COllection Filter()
import
java.util.stream.Collectors;
import
java.util.*;
class
GFG {
public
static
void
main(String[] args)
{
// Create the list with null values
List<String> list =
new
ArrayList<>(
Arrays.asList(
"Geeks"
,
null
,
"forGeeks"
,
null
,
"A computer portal"
));
// Print the list
System.out.println(
"Initial List: "
+ list);
// Removing nulls using Java Stream
// using Predicate condition in lambda expression
list = list.stream()
.filter(x -> x !=
null
)
.collect(Collectors.toList());
// Print the list
System.out.println(
"Modified List: "
+ list);
}
}
输出:Initial List: [Geeks, null, forGeeks, null, A computer portal] Modified List: [Geeks, forGeeks, A computer portal]
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END