Java 流的中级方法

Java 流的中级方法

在Java中,流允许我们对指定的元素执行功能操作。它简单地将源(如数组、文件和集合框架的类)的元素通过各种内置方法进行通道化,以返回结果。这些内置方法可以是中间方法或终端方法。在本文中,我们将探讨一些流的中级方法,如map、filter、reduce和collect。这些方法帮助我们操纵和处理数据。

Java流的中级方法

Java流的方法被统称为高阶函数,进一步被分类为:

  • 中间方法 - 它们处理输入流的元素。

  • 终端方法 - 它们触发中间操作以生成非流结果,如打印、计数和存储。

在本节中,我们将通过各种示例仅讨论流的中级方法。

Java中的流的中级方法列表如下:

  • filter()

  • map()

  • peek()

  • limit()

  • skip()

  • sorted()

  • distinct()

让我们逐个讨论它们。

filter()方法

Java的filter()方法允许我们根据指定的条件对流的元素进行过滤。它用于对流项应用某种行为。该方法接受一个断言作为参数,并返回与断言匹配的元素列表。

语法

filter(predicate);

示例

以下示例演示了filter()方法的使用。

  • 使用Arrays.asList()方法创建一个固定大小的列表。

  • 现在,使用filter()方法以及stream()和forEach(),只过滤出奇数。在这里,stream()指定输入的流形式,我们将使用终端方法forEach()来迭代和打印奇数。

import java.util.*;
public class Fltr {
   public static void main(String[] args) {
      // creating a list of numbers
      List<Integer> numbers = Arrays.asList(5, 21, 32, 14, 63, 19, 10);
      // printing all numbers in the list
      System.out.println("List of all numbers: " + numbers);
      System.out.println("List of odd numbers: ");
      // filtering only odd numbers from list
      numbers.stream()
         .filter(nums -> nums % 2 == 1)
         .forEach(System.out::println);
   }
}

输出

List of all numbers: [5, 21, 32, 14, 63, 19, 10]
List of odd numbers: 
5
21
63
19

map()方法

该方法将一个函数作为参数,并将其应用于流的每个元素,从而产生一个映射后的元素流。例如,我们可以使用map()方法将一个字符串流转换为一个整数流。

示例

在这个示例中,我们将使用map()方法将一个列表的元素复制到另一个列表中。

import java.util.*;
import java.util.stream.Collectors;
public class MapExample {
   public static void main(String[] args) {
      // creating a list of integers
      List<Integer> AList1 = Arrays.asList(11, 22, 33, 44, 55);
      // to copy elements of first list to new list 
      List<Integer> AList2 = AList1.stream()
         // copying the elements
         .map(i -> i) 
         // collecting the result
         .collect(Collectors.toList()); 
      // Printing the result
      System.out.println("The elements of the list: " + AList2);
   }
}

输出

The elements of the list: [11, 22, 33, 44, 55]

peek()方法

我们可以使用peek()方法来打印每个中间操作的结果。

示例

在下面的示例中,我们将使用filter()方法和peek()方法来打印一个数字列表中的奇数。

import java.util.*;
import java.util.stream.Collectors;
public class PeekExample {
   public static void main(String[] args) {
      // creating a list of numbers
      List<Integer> numbers = Arrays.asList(5, 21, 32, 14, 63, 19, 10);
      // printing all numbers in the list
      System.out.println("List of all numbers: " + numbers);
      System.out.println("List of odd numbers: ");
      // filtering only odd numbers from list
      numbers.stream()
         .filter(nums -> nums % 2 == 1)
         // using peek to get the result
         .peek(nums -> System.out.println(nums))
         // collecting the result
         .collect(Collectors.toList()); 
   }
}

输出

List of all numbers: [5, 21, 32, 14, 63, 19, 10]
List of odd numbers: 
5
21
63
19

limit()方法

使用limit()方法的目的是限制输出结果的大小至指定的限制。

示例

在该示例中,我们将借助limit()方法将中间操作的结果限制在指定的大小。

import java.util.*;
public class LimitExample {
   public static void main(String[] args) {
      // creating a list of numbers
      List<Integer> numbers = Arrays.asList(5, 21, 32, 14, 63, 19, 10);
      // printing all numbers in the list
      System.out.println("List of all numbers: " + numbers);
      System.out.println("List of odd numbers: ");
      // filtering only odd numbers from list
      numbers.stream()
         .filter(nums -> nums % 2 == 1)
         // limiting the result to 2 only
         .limit(2)
         .forEach(System.out::println);
   }
}

输出结果

List of all numbers: [5, 21, 32, 14, 63, 19, 10]
List of odd numbers: 
5
21

skip()方法

我们可以使用skip()方法从结果中舍弃前n个元素。它接受一个整数作为参数,指定要舍弃的元素数量。

示例

以下示例演示如何使用skip()方法从过滤后的结果中舍弃前两个元素。

import java.util.*;
public class SkipExample {
   public static void main(String[] args) {
      // creating a list of numbers
      List<Integer> numbers = Arrays.asList(5, 21, 32, 14, 63, 19, 10);
      // printing all numbers in the list
      System.out.println("List of all numbers: " + numbers);
      System.out.println("List of odd numbers: ");
      // filtering only odd numbers from list
      numbers.stream()
         .filter(nums -> nums % 2 == 1)
         // skipping first 2 elements from the result
         .skip(2)
         .forEach(System.out::println);
   }
}

输出

List of all numbers: [5, 21, 32, 14, 63, 19, 10]
List of odd numbers: 
63
19

sorted()方法

该方法可以用于按升序对流中的元素进行排序。

示例

下面的示例演示了如何使用sorted()方法按升序对流的元素进行排序。

import java.util.*;
public class SortedExample {
   public static void main(String[] args) {
      // creating a list of numbers
      List<Integer> numbers = Arrays.asList(5, 21, 32, 14, 63, 19, 10);
      // printing all numbers from the list in ascending order 
      System.out.println("List of numbers: ");
      // Sorting the elements of list
      numbers.stream()
         .sorted()
         .forEach(System.out::println);
   }
}

输出

List of numbers: 
5
10
14
19
21
32
63

distinct()方法

使用distinct()方法可以去除流中元素的重复性。

示例

在这个例子中,我们将创建一个带有重复元素的流,并应用distinct()方法仅打印出不同的元素。

import java.util.*;
public class DistinctExample {
   public static void main(String[] args) {
      // creating a list of numbers
      List<Integer> numbers = Arrays.asList(21, 21, 32, 14, 19, 19, 10, 10);
      System.out.println("Distinct list of numbers: ");
      // removing duplicate numbers from the list 
      numbers.stream()
         .distinct()
         .forEach(System.out::println);
   }
}

输出

Distinct list of numbers: 
21
32
14
19
10

结论

我们从定义流及其在Java中作为高阶函数的一部分的中间方法开始了本文。在下一部分中,我们通过示例程序详细讨论了中间方法。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程