如何在Java 8中打印Stream的元素?

流的打印元素 (Printing elements of a Stream)

In Java, there are three different ways to print the elements of a Stream in java 8. The name of these 3 different ways is given below:

在Java中,有三种不同的方法可以在java 8中打印Stream的元素 。 这三种不同方式的名称如下:

  1. forEach() method of Stream

    Stream的forEach()方法

  2. println() with collect() method of Stream

    使用Stream的collect()方法的println()

  3. peek() method method of Stream

    Stream的peek()方法

We will see all the three ways of printing the elements of a stream in java 8 one by one...

我们将看到在Java 8中一对一打印流元素的所有三种方式...

1)Stream的forEach()方法 (1) forEach() method of Stream)

  • This method is available in java.util.stream package.

    该方法在java.util.stream包中可用。

  • This is not the static method so this method will be called with the object.

    这不是静态方法,因此将与对象一起调用此方法。

  • The return type of this method is void so it does not return anything.

    此方法的返回类型为void,因此它不返回任何内容。

  • This method acts as each element of the stream.

    此方法充当流的每个元素。

The syntax of this method is given below,

该方法的语法如下所示:

    void forEach(Consumer <? super T > consumer);

Here, Consumer is an interface and T is the element type.

这里, Consumer是一个接口, T是元素类型。

Example: Non lambda expression

示例:非lambda表达式

import java.util.stream.*;

public class PrintStreamElementByForeachMethod {
    public static void main(String[] args) {
        // Here of() method of Stream interface is used to get the stream
        Stream stm = Stream.of("Java", "is", "a", "programming", "language");

        // we are printing the stream by using forEach() method
        stm.forEach(stm1 -> System.out.println(stm1));
    }
}

Output

输出量

E:\Programs>javac PrintStreamElementByForeachMethod.java
E:\Programs>java PrintStreamElementByForeachMethod
Java
is
a
programming
language

Example: Short hand lambda expression

示例:简短的lambda表达式

import java.util.stream.*;

public class PrintStreamElementByForeachMethod {
    public static void main(String[] args) {
        // Here of() method of Stream interface is used to get the stream
        Stream stm = Stream.of("Java", "is", "a", "programming", "language");

        // we are printing the stream by using forEach() method
        stm.forEach(System.out::println);
    }
}

Output

输出量

E:\Programs>javac PrintStreamElementByForeachMethod.java

E:\Programs>java PrintStreamElementByForeachMethod
Java
is
a
programming
language

2)使用Stream的collect()方法的println() (2) println() with collect() method of Stream)

  • This method is available in java.util.stream package.

    该方法在java.util.stream包中可用。

  • This method is not static so it will be accessible with objects of Stream interface.

    此方法不是静态的,因此可以通过Stream接口的对象进行访问。

  • This method collects stream elements as a Collector object and then print the elements by using println() method.

    此方法将流元素收集为Collector对象,然后使用println()方法打印这些元素。

The syntax of println() with collect() method,

println()与collect()方法的语法,

    System.out.println(Stream_object.collect(Collectors.toList()));

Example:

例:

import java.util.stream.*;

public class PrintStreamElementByForeachMethod {
    public static void main(String[] args) {
        // Here of() method of Stream interface is used to get the stream
        Stream stm = Stream.of("Java", "is", "a", "programming", "language");

        // we are printing the stream by using forEach() method
        stm.forEach(System.out::println);
    }
}

Output

输出量

E:\Programs>javac PrintStreamElementByForeachMethod.java

E:\Programs>java PrintStreamElementByForeachMethod
[Java, is, a, programming, language]

3)Stream的peek()方法 (3) peek() method method of Stream)

  • This method is available in java.util.stream package.

    该方法在java.util.stream包中可用。

  • This method is not static so this method will be called with the object of Stream.

    此方法不是静态的,因此将与Stream对象一起调用此方法。

The syntax of this method is given below:

该方法的语法如下:

    Stream peek(Consumer <? super T> consumer);

This method returns a Stream and it consists of all the elements of the Current stream and it performs the given operation or action on each element.

此方法返回一个Stream,它由Current流的所有元素组成,并且对每个元素执行给定的操作或动作。

In this method, if a stream is already consumed then the same stream we want to consume again then, in that case, we will not get any error or exception and it is valid.

在这种方法中,如果已经消耗了一个流,那么我们要再次消耗相同的流,那么在这种情况下,我们将不会得到任何错误或异常,并且它是有效的。

Example:

例:

import java.util.stream.*;

public class PrintStreamElementByPeekMethod {
    public static void main(String[] args) {
        // Here of() method of Stream interface is used to get the stream
        Stream stm = Stream.of("Java", "is", "a", "programming", "language");

        //  we are printing the stream by using peek() method 
        //  and it provides the facility count() method to terminate 
        stm.peek(stm1 -> System.out.println(stm1)).count();
    }
}

Output

输出量

E:\Programs>javac PrintStreamElementByPeekMethod.java

E:\Programs>java PrintStreamElementByPeekMethod
Java
is
a
programming
language


翻译自: https://www.includehelp.com/java/how-to-print-elements-of-a-stream-in-java-8.aspx

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值