java8stream如何打印数据元素

2022-11-17 09:14:02
目录
写在前面利用stream打印元素1. forEach() 方法2. println() with collect()方法3. peek() 方法

写在前面

在这里,我们将会学习怎么利用java8>

利用stream打印元素

在Java中,有三种不同的方法来打印Java>

这三种不同方式的名称如下

    Stream的 forEach()方法Stream的 println()方法和collect()方法Stream的 peek()方法

    我们将在java 8中逐一看到打印流元素的三种方法……

    1.>
      该方法在java.util.stream包中可用。这不是静态方法,所以这个方法将被对象调用。这个方法的返回类型是void,所以它不返回任何东西。这个方法充当流的每个元素。

      这个方法的语法如下所示:

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

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

      示例:不用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));
          }
      }

      输出

      Java
      is
      a
      programming
      language

      示例:简写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);
          }
      }

      输出

      Java
      is
      a
      programming
      language

      2.>

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

      这个方法不是静态的,所以可以通过Stream接口的对象访问。

      该方法以Collector对象的形式收集流元素,然后使用println()方法打印元素。

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

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

      示例

      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);
          }
      }

      输出

      [Java, is, a, programming, language]

      3.>

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

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

      这个方法的语法如下所示

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

      这个方法返回一个Stream,它包含Current流的所有元素,并对每个元素执行给定的操作或动作。

      在这个方法中,如果一个流已经被消费,那么我们希望再次消费的流,在这种情况下,我们将不会得到任何错误或异常,而且它是有效的。

      示例

      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();
          }
      }

      输出

      Java
      is
      a
      programming
      language

      以上为个人经验,希望能给大家一个参考,也希望大家多多支持易采站长站。