📅  最后修改于: 2020-03-28 14:21:34             🧑  作者: Mango
Java 8中引入了Stream API,用于处理对象的集合。流是支持各种方法的对象序列,可以将这些方法通过管道传输以产生所需的结果。
Java流的功能是:
有3种方法来打印Java流元素:
以下是详细打印Stream的三种方法:
void forEach(Consumer super T> action)
Consumer是功能性接口
T是stream的元素类型.
以下是如何使用forEach()方法打印Stream的元素:
程序1:
// Java展示打印stream中的元素
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// 获取stream
Stream stream = Stream.of("芒果", "For",
"芒果", "A",
"Computer", "Portal");
// 打印stream
stream.forEach(s -> System.out.println(s));
}
}
输出:
芒果
For
芒果
A
Computer
Portal
程序2:使用lambda表达式
// Java展示打印Stream
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// 获取stream
Stream stream = Stream.of("芒果", "For",
"芒果", "A",
"Computer", "Portal");
// 打印stream
stream.forEach(System.out::println);
}
}
输出:
芒果
For
芒果
A
Computer
Portal
程序3:这种方法消耗了流,并使其无法将来使用。因此,以下代码将引发错误,因为流已被使用。
// Java展示打印Stream
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// 获取stream
Stream stream = Stream.of("芒果", "For",
"芒果", "A",
"Computer", "Portal");
// 打印stream
stream.forEach(s -> System.out.println(s));
// Since the stream has been already consumed
// this will throw exception
try {
// 打印stream
stream.forEach(s -> System.out.println(s));
}
catch (Exception e) {
System.out.println("\n报错: " + e);
}
}
}
输出:
芒果
For
芒果
A
Computer
Portal
报错: java.lang.IllegalStateException:
stream has already been operated upon or closed
System.out.println(stream.collect(Collectors.toList()));
程序1:
// Java展示打印stream的元素
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// 获取stream
Stream stream = Stream.of("芒果", "For",
"芒果", "A",
"Computer", "Portal");
// 打印stream
System.out.println(stream.collect(Collectors.toList()));
}
}
输出:
[芒果, For, 芒果, A, Computer, Portal]
程序2:此方法还会消耗该流,并使其无法将来使用。因此,以下代码将引发错误,因为流已被使用。
// Java展示打印stream的元素
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// 获取stream
Stream stream = Stream.of("芒果", "For",
"芒果", "A",
"Computer", "Portal");
// 打印stream
System.out.println(stream.collect(Collectors.toList()));
// Since the stream has been already consumed
// this will throw exception
try {
// 打印stream
System.out.println(stream.collect(Collectors.toList()));
}
catch (Exception e) {
System.out.println("\n报错: " + e);
}
}
}
输出:
[芒果, For, 芒果, A, Computer, Portal]
报错: java.lang.IllegalStateException:
stream has already been operated upon or closed
Stream
peek(Consumer super T> action)
Stream是一个接口, T是流的元素数据类型
action是非接口的操作
用来在消耗的元素上进行操作
这个函数返回新的流
程序1:
// Java展示打印流
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// 获取stream
Stream stream = Stream.of("芒果", "For",
"芒果", "A",
"Computer", "Portal");
// 打印stream,使用peek()
// 提供count()
stream.peek(s -> System.out.println(s)).count();
}
}
输出:
芒果
For
芒果
A
Computer
Portal
程序2:此方法不消耗流。因此,以下代码不会引发任何错误。
// Java打印流的元素
import java.util.stream.*;
class GFG {
public static void main(String[] args)
{
// 获取stream
Stream stream = Stream.of("Geeks", "For",
"GeeksForGeeks", "A",
"Computer", "Portal");
// 打印流
stream.filter(s -> s.startsWith("G"))
.peek(s -> System.out.println("过滤的值: " + s))
.map(String::toUpperCase)
.peek(s -> System.out.println("大写值 :" + s))
.count();
}
}
输出:
Filtered value: Geeks
Uppercase value :GEEKS
Filtered value: GeeksForGeeks
Uppercase value :GEEKSFORGEEKS