从列表中删除满足Java中给定谓词的元素
以下是从满足 Predicate 条件的 List 中有效删除元素的方法:
p ==> Predicate, specifying the condition
l ==> List, from which element to be removed
使用迭代器
下面的程序演示了使用 Predicate 从列表中删除空元素
Java
// Java Program to remove nulls
// from a List using iterator and Predicate
import java.util.function.Predicate;
import java.util.*;
class GFG {
// Generic function to remove Null Using Iterator
public static List
removeNullUsingIterator(List l, Predicate p)
{
// Create an iterator from the l
Iterator itr = l.iterator();
// Find and remove all null
while (itr.hasNext()) {
// Fetching the next element
T t = itr.next();
// Checking for Predicate condition
if (!p.test(t)) {
// If the condition matches,
// remove that element
itr.remove();
}
}
// Return the null
return l;
}
public static void main(String[] args)
{
// Create the l with null values
List l = new ArrayList<>(
Arrays.asList("Geeks",
null,
"forGeeks",
null,
"A computer portal"));
// Print the list
System.out.println("List with null values: " + l);
// Creating a Predicate condition checking for null
Predicate isNull = item -> Objects.nonNull(item);
// Removing nulls using iterator and Predicate
l = removeNullUsingIterator(l, isNull);
// Print the list
System.out.println("List with null values removed: " + l);
}
}
Java
// Java Program to remove 10
// from a List using List.removeAll() and Predicate
import java.util.function.Predicate;
import java.util.*;
class GFG {
// Generic function to remove elements using Predicate
public static List
removeElements(List l, Predicate p)
{
// Create collection using Predicate
Collection collection = new ArrayList<>();
for (T t : l) {
if (p.test(t)) {
collection.add(t);
}
}
// Print the list
System.out.println("Collection to be removed: " + collection);
// Removing 10 using List.removeAll()
// passing a collection
l.removeAll(collection);
// Return the list
return l;
}
public static void main(String[] args)
{
// Create a list with null values
List l = new ArrayList<>(
Arrays.asList("1", "10", "15", "10", "12", "5", "10", "20"));
// Print the list
System.out.println("Original List: " + l);
// Creating a Predicate condition checking for 10
Predicate is10 = i -> (i == "10");
// Removing using Predicate
l = removeElements(l, is10);
// Print the list
System.out.println("Updated List: " + l);
}
}
Java
// Java Program to remove nulls
// from a List using Java 8
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.*;
class GFG {
// Generic function to remove elements using Predicate
public static List
removeElements(List l, Predicate p)
{
// Removing nulls using Java Stream
// using Predicate condition in lambda expression
l = l.stream()
.filter(p)
.collect(Collectors.toList());
// Return the list
return l;
}
public static void main(String[] args)
{
// Create a list with null values
List l = new ArrayList<>(
Arrays.asList("Geeks",
null,
"forGeeks",
null,
"A computer portal"));
// Print the list
System.out.println("List with null values: " + l);
// Creating a Predicate condition checking for null
Predicate isNull = i -> (i != null);
// Removing using Predicate
l = removeElements(l, isNull);
// Print the list
System.out.println("List with null values removed: " + l);
}
}
Java
// Java Program to remove nulls
// from a List using Java 8
import java.util.function.Predicate;
import java.util.*;
class GFG {
// Generic function to remove elements using Predicate
public static List
removeElements(List l, Predicate p)
{
// Removing nulls using Java Stream
// using Predicate condition in removeIf()
l.removeIf(x -> p.test(x));
// Return the list
return l;
}
public static void main(String[] args)
{
// Create a list with null values
List l = new ArrayList<>(
Arrays.asList("Geeks",
null,
"forGeeks",
null,
"A computer portal"));
// Print the list
System.out.println("List with null values: " + l);
// Creating a Predicate condition checking for null
Predicate isNull = i -> (i == null);
// Removing using Predicate
l = removeElements(l, isNull);
// Print the list
System.out.println("List with null values removed: " + l);
}
}
输出
List with null values: [Geeks, null, forGeeks, null, A computer portal]
List with null values removed: [Geeks, forGeeks, A computer portal]
使用 List.removeAll()
在此方法中,包含要删除的元素的集合用于从原始 l 中删除这些元素。它需要使用 Predicate 条件创建包含所需元素的集合。完成此操作后,将在原始 l 中搜索此集合并删除它的所有实例。
Java
// Java Program to remove 10
// from a List using List.removeAll() and Predicate
import java.util.function.Predicate;
import java.util.*;
class GFG {
// Generic function to remove elements using Predicate
public static List
removeElements(List l, Predicate p)
{
// Create collection using Predicate
Collection collection = new ArrayList<>();
for (T t : l) {
if (p.test(t)) {
collection.add(t);
}
}
// Print the list
System.out.println("Collection to be removed: " + collection);
// Removing 10 using List.removeAll()
// passing a collection
l.removeAll(collection);
// Return the list
return l;
}
public static void main(String[] args)
{
// Create a list with null values
List l = new ArrayList<>(
Arrays.asList("1", "10", "15", "10", "12", "5", "10", "20"));
// Print the list
System.out.println("Original List: " + l);
// Creating a Predicate condition checking for 10
Predicate is10 = i -> (i == "10");
// Removing using Predicate
l = removeElements(l, is10);
// Print the list
System.out.println("Updated List: " + l);
}
}
输出
Original List: [1, 10, 15, 10, 12, 5, 10, 20]
Collection to be removed: [10, 10, 10]
Updated List: [1, 15, 12, 5, 20]
使用 Lambda (Java 8)
Stream.filter() 方法可以在Java 8 中使用,它返回一个由元素组成的流
匹配给定的谓词条件。
Java
// Java Program to remove nulls
// from a List using Java 8
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.*;
class GFG {
// Generic function to remove elements using Predicate
public static List
removeElements(List l, Predicate p)
{
// Removing nulls using Java Stream
// using Predicate condition in lambda expression
l = l.stream()
.filter(p)
.collect(Collectors.toList());
// Return the list
return l;
}
public static void main(String[] args)
{
// Create a list with null values
List l = new ArrayList<>(
Arrays.asList("Geeks",
null,
"forGeeks",
null,
"A computer portal"));
// Print the list
System.out.println("List with null values: " + l);
// Creating a Predicate condition checking for null
Predicate isNull = i -> (i != null);
// Removing using Predicate
l = removeElements(l, isNull);
// Print the list
System.out.println("List with null values removed: " + l);
}
}
输出
List with null values: [Geeks, null, forGeeks, null, A computer portal]
List with null values removed: [Geeks, forGeeks, A computer portal]
使用 removeIf()
顾名思义,这是一种删除所有满足给定谓词的元素的直接方法。
Java
// Java Program to remove nulls
// from a List using Java 8
import java.util.function.Predicate;
import java.util.*;
class GFG {
// Generic function to remove elements using Predicate
public static List
removeElements(List l, Predicate p)
{
// Removing nulls using Java Stream
// using Predicate condition in removeIf()
l.removeIf(x -> p.test(x));
// Return the list
return l;
}
public static void main(String[] args)
{
// Create a list with null values
List l = new ArrayList<>(
Arrays.asList("Geeks",
null,
"forGeeks",
null,
"A computer portal"));
// Print the list
System.out.println("List with null values: " + l);
// Creating a Predicate condition checking for null
Predicate isNull = i -> (i == null);
// Removing using Predicate
l = removeElements(l, isNull);
// Print the list
System.out.println("List with null values removed: " + l);
}
}
输出
List with null values: [Geeks, null, forGeeks, null, A computer portal]
List with null values removed: [Geeks, forGeeks, A computer portal]