使用 Stream API 和示例在Java 8+ 中进行函数式编程
API 是应用程序编程接口的首字母缩写词,它是软件和Java流在数据源上工作。考虑一条小溪,就像一条小运河中的水流。让我们举一个现实生活中的例子。每次用户使用应用程序时,WhatsApp 都会流行起来,以便通过向其他用户发送短信或电话进行交流。两个用户都在使用 API。
Java流处理三种操作,如下所述
- 数据源
- 中间操作
- 终端操作
方法:可以通过三种方式创建流
- 使用集合框架中任何类的对象
- 使用引用数据类型的数组
- 使用“Java.util.stream”包中定义的接口
方法一:数据源
数据源可以多种多样,例如数组、列表等
句法:
ArrayList numbers = new ArrayList<>();
Integer[] numbers = {1,2,3};
示例 1:使用对象作为数据源
Java
// Java Program showcasing data source
// using an object as a data source
// Importing input output classes
import java.io.*;
// Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Data Source
// Creating an arrayList object
// Declaring object of String type
ArrayList gfgNames = new ArrayList<>();
// Custom input elements to above object
gfgNames.add("Dean");
gfgNames.add("castee");
gfgNames.add("robert");
// Creating object of Stream where Stream is created
// using arrayList and object as data source
Stream streamOfNames = gfgNames.stream();
// Print and display element
System.out.print(streamOfNames);
}
}
Java
// Java Program showcasing data source
// using an array as a data source
// Importing java input output class
import java.io.*;
// Importing all classes from
// java.util package
import java.util.*;
// Importing class for additional operations,
// additionls and pipelines
import java.util.stream.IntStream;
// Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a predefined stream using range method
// Custom inputs for range as parameters
var stream = IntStream.range(1, 100);
// Stream is created
var max = stream.filter(number -> number % 4 == 0)
.count();
// Print and display the maximum number
// from the stream
System.out.println(max);
}
}
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
// Data Source
Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }
// Stream
Stream streamOfNumbers
= Arrays.stream(numbers);
// filter all the even numbers
Stream evenNumbersStream
= streamOfNumbers.filter(
number -> number % 2 == 0)
}
}
Java
// Importing input output classes
import java.io.*;
// Importing all classes from
// java.util package
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Data source
// Custom integer inputs
Integer[] numbers
= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
// Stream
var streamOfNumbers = Arrays.stream(numbers);
// Using filter method
var evenNumbersStream
= streamOfNumbers
.filter(number -> number % 3 == 0)
.count();
// Print and display
System.out.println(evenNumbersStream);
}
}
Java
// Importing all classes from java.util package
import java.io.*;
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an ArrayList of Integer type
ArrayList list = new ArrayList<>();
// Adding elements to above object of Arrayist
// Custom inputs
list.add(20);
list.add(4);
list.add(76);
list.add(21);
list.add(3);
list.add(80);
var stream = list.stream();
var numbers
= stream.filter(number -> number % 2 == 0)
.filter(number -> number > 20);
// Print all the elements of the stream on the console
numbers.forEach(System.out::println);
}
}
示例 2:使用数组作为数据源
// Data Source
Integer[] numbers = {1,2,3,4,5};
// Stream using an array
Stream streamOfNumbers = Arrays.stream(numbers);
// using predefined Instream interface
integerStream = IntStream.range(1,100); // a stream from 1 to 99;
Java
// Java Program showcasing data source
// using an array as a data source
// Importing java input output class
import java.io.*;
// Importing all classes from
// java.util package
import java.util.*;
// Importing class for additional operations,
// additionls and pipelines
import java.util.stream.IntStream;
// Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating a predefined stream using range method
// Custom inputs for range as parameters
var stream = IntStream.range(1, 100);
// Stream is created
var max = stream.filter(number -> number % 4 == 0)
.count();
// Print and display the maximum number
// from the stream
System.out.println(max);
}
}
24
方法二:中间操作
中间操作是一些可以应用于流的方法。
Note: It can be of any number
filter()
例子:
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
// Data Source
Integer[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }
// Stream
Stream streamOfNumbers
= Arrays.stream(numbers);
// filter all the even numbers
Stream evenNumbersStream
= streamOfNumbers.filter(
number -> number % 2 == 0)
}
}
方法三:终端操作
我们可以在流上应用的终端操作方法会导致流被“关闭”。
概念:
一些终端操作可用于迭代流的元素。
min(),max(),count()
forEach(),noneMatch()
示例 1:解释流 API
Java
// Importing input output classes
import java.io.*;
// Importing all classes from
// java.util package
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Data source
// Custom integer inputs
Integer[] numbers
= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
// Stream
var streamOfNumbers = Arrays.stream(numbers);
// Using filter method
var evenNumbersStream
= streamOfNumbers
.filter(number -> number % 3 == 0)
.count();
// Print and display
System.out.println(evenNumbersStream);
}
}
输出
4
示例 2:
Java
// Importing all classes from java.util package
import java.io.*;
import java.util.*;
// Class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Creating an ArrayList of Integer type
ArrayList list = new ArrayList<>();
// Adding elements to above object of Arrayist
// Custom inputs
list.add(20);
list.add(4);
list.add(76);
list.add(21);
list.add(3);
list.add(80);
var stream = list.stream();
var numbers
= stream.filter(number -> number % 2 == 0)
.filter(number -> number > 20);
// Print all the elements of the stream on the console
numbers.forEach(System.out::println);
}
}
输出
76
80
Note: One can pass lambda also number -> System.out.println(number + ” “)