📜  Java中的 Stream filter() 示例

📅  最后修改于: 2022-05-13 01:55:45.098000             🧑  作者: Mango

Java中的 Stream filter() 示例

流过滤器(谓词谓词)返回一个流,该流由该流中与给定谓词匹配的元素组成。这是一个中间操作。这些操作总是惰性的,即执行诸如 filter() 之类的中间操作实际上并不执行任何过滤,而是创建一个新流,当遍历该流时,它包含与给定谓词匹配的初始流的元素。

句法:

Stream filter(Predicate predicate)

其中 Stream 是一个接口, T 是谓词的输入类型。

返回类型:一个新的流。

执行:

  1. 过滤掉可被 0 到 10 之间的某个特定数字整除的元素。
  2. 过滤掉任何特定索引处带有大写字母的元素。
  3. 过滤掉以自定义字母结尾的元素。

示例 1: filter() 方法,过滤掉可被 5 整除的元素。

Java
// Java Program to get a Stream Consisting of the Elements
// of Stream that Matches Given Predicate for Stream filter
// (Predicate predicate)
  
// Importing required classes
import java.util.*;
  
// Class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating a list of Integers
        List list = Arrays.asList(3, 4, 6, 12, 20);
  
        // Getting a stream consisting of the
        // elements that are divisible by 5
        // Using Stream filter(Predicate predicate)
        list.stream()
            .filter(num -> num % 5 == 0)
            .forEach(System.out::println);
    }
}


Java
// Java Program to Get Stream Consisting of Elements
// of Stream that Matches Given Predicate
// for Stream Filter (Predicate predicate)
  
// Importing required classes
import java.util.stream.Stream;
  
// Class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a stream of strings
        Stream stream = Stream.of(
            "Geeks", "fOr", "GEEKSQUIZ", "GeeksforGeeks");
  
        // Getting a stream consisting of the
        // elements having UpperCase Character
        // at custom index say be it '1'
        // using Stream filter(Predicate predicate)
        stream
            .filter(
                str -> Character.isUpperCase(str.charAt(1)))
            .forEach(System.out::println);
    }
}


Java
// Java Program to Get a Stream Consisting ofElements
//  of Stream that Matches Given predicate
// for Stream filter (Predicate predicate)
  
// Importing required classes
import java.util.stream.Stream;
  
// Class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating a stream of strings
        Stream stream = Stream.of(
            "Geeks", "foR", "GeEksQuiz", "GeeksforGeeks");
  
        // Getting a stream consisting of the
        // elements ending with 's'
        // using Stream filter(Predicate predicate)
        stream.filter(str -> str.endsWith("s"))
            .forEach(System.out::println);
    }
}


输出
20

示例 2: filter() 方法,其操作是过滤掉索引 1 处具有大写字母的元素。

Java

// Java Program to Get Stream Consisting of Elements
// of Stream that Matches Given Predicate
// for Stream Filter (Predicate predicate)
  
// Importing required classes
import java.util.stream.Stream;
  
// Class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
        // Creating a stream of strings
        Stream stream = Stream.of(
            "Geeks", "fOr", "GEEKSQUIZ", "GeeksforGeeks");
  
        // Getting a stream consisting of the
        // elements having UpperCase Character
        // at custom index say be it '1'
        // using Stream filter(Predicate predicate)
        stream
            .filter(
                str -> Character.isUpperCase(str.charAt(1)))
            .forEach(System.out::println);
    }
}
输出
fOr
GEEKSQUIZ

示例 3: filter() 方法具有过滤掉以自定义字母结尾的元素的操作,说它是 's' 以用于实现目的。

Java

// Java Program to Get a Stream Consisting ofElements
//  of Stream that Matches Given predicate
// for Stream filter (Predicate predicate)
  
// Importing required classes
import java.util.stream.Stream;
  
// Class
class GFG {
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating a stream of strings
        Stream stream = Stream.of(
            "Geeks", "foR", "GeEksQuiz", "GeeksforGeeks");
  
        // Getting a stream consisting of the
        // elements ending with 's'
        // using Stream filter(Predicate predicate)
        stream.filter(str -> str.endsWith("s"))
            .forEach(System.out::println);
    }
}
输出
Geeks
GeeksforGeeks