个性化阅读
专注于IT技术分析

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

在Java 8中引入Stream API,它用于处理对象的集合。Stream是支持各种方法的对象序列, 可以对这些方法进行流水线处理以产生所需的结果。

Java Stream的功能是–

  • Stream不是数据结构, 而是从集合, 数组或I/O通道获取输入。
  • Stream不会改变原始数据结构, 它们只会按照流水线方法提供结果。
  • 每个中间操作都是延迟执行的, 并因此返回一个流, 因此可以对各种中间操作进行流水线处理。终端操作标记流的结尾并返回结果。

有3种方法可以用Java打印Stream元素:

  1. forEach()
  2. println()与collect()
  3. peek()

以下是详细打印Stream的三种方法:

Stream forEach(消费者动作):这个方法对流的每个元素执行一个动作。流forEach(消费者操作)是一个终端操作,也就是说,它可以遍历流来产生结果或副作用。

语法 :

void forEach(Consumer<? super T> action)

Where, Consumer is a functional interface
and T is the type of stream elements.

以下是使用forEach()方法打印Stream元素的方法:

程序1:

// Java code to print the elements of Stream
  
import java.util.stream.*;
  
class GFG {
     public static void main(String[] args)
     {
  
         // Get the stream
         Stream<String> stream = Stream.of( "Geeks" , "For" , "Geeks" , "A" , "Computer" , "Portal" );
  
         // Print the stream
         stream.forEach(s -> System.out.println(s));
     }
}

输出如下:

Geeks
For
Geeks
A
Computer
Portal

程序2:使用Short Hand Lambda表达式

// Java code to print the elements of Stream
  
import java.util.stream.*;
  
class GFG {
     public static void main(String[] args)
     {
  
         // Get the stream
         Stream<String> stream = Stream.of( "Geeks" , "For" , "Geeks" , "A" , "Computer" , "Portal" );
  
         // Print the stream
         stream.forEach(System.out::println);
     }
}

输出如下:

Geeks
For
Geeks
A
Computer
Portal

程序3:这种方法消耗了Stream, 并使其无法将来使用。因此, 以下代码将引发错误, 因为Stream已被使用。

// Java code to print the elements of Stream
  
import java.util.stream.*;
  
class GFG {
     public static void main(String[] args)
     {
  
         // Get the stream
         Stream<String> stream = Stream.of( "Geeks" , "For" , "Geeks" , "A" , "Computer" , "Portal" );
  
         // Print the stream
         stream.forEach(s -> System.out.println(s));
  
         // Since the stream has been already consumed
         // this will throw exception
         try {
  
             // Print the stream
             stream.forEach(s -> System.out.println(s));
         }
  
         catch (Exception e) {
  
             System.out.println( "\nException: " + e);
         }
     }
}

输出如下:

Geeks
For
Geeks
A
Computer
Portal

Exception: java.lang.IllegalStateException: 
stream has already been operated upon or closed

将println()与collect()结合使用:

此方法收集Stream元素作为收集器实例, 例如List。因此, 使用println()方法可以轻松完成List的打印。

语法如下:

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

程序1:

// Java code to print the elements of Stream
  
import java.util.stream.*;
  
class GFG {
     public static void main(String[] args)
     {
  
         // Get the stream
         Stream<String> stream = Stream.of( "Geeks" , "For" , "Geeks" , "A" , "Computer" , "Portal" );
  
         // Print the stream
         System.out.println(stream.collect(Collectors.toList()));
     }
}

输出如下:

[Geeks, For, Geeks, A, Computer, Portal]

程序2:这种方法还会消耗该Stream, 并使其无法将来使用。因此, 以下代码将引发错误, 因为Stream已被使用。

// Java code to print the elements of Stream
  
import java.util.stream.*;
  
class GFG {
     public static void main(String[] args)
     {
  
         // Get the stream
         Stream<String> stream = Stream.of( "Geeks" , "For" , "Geeks" , "A" , "Computer" , "Portal" );
  
         // Print the stream
         System.out.println(stream.collect(Collectors.toList()));
  
         // Since the stream has been already consumed
         // this will throw exception
         try {
  
             // Print the stream
             System.out.println(stream.collect(Collectors.toList()));
         }
  
         catch (Exception e) {
  
             System.out.println( "\nException: " + e);
         }
     }
}

输出如下:

[Geeks, For, Geeks, A, Computer, Portal]

Exception: java.lang.IllegalStateException: 
stream has already been operated upon or closed

Stream peek(消费者行为):此方法返回由该流的元素组成的流, 并在从结果流中消耗元素时对每个元素另外执行提供的操作。这是个中间操作,即, 它创建了一个新的流, 该流在遍历时将包含与给定谓词匹配的初始流的元素。

语法 :

Stream<T> 
    peek(Consumer<? super T> action)

Where, Stream is an interface and T is the type of 
stream elements. action is a non-interfering action
to perform on the elements as they are consumed 
from the stream and the function returns the new stream.

程序1:

// Java code to print the elements of Stream
  
import java.util.stream.*;
  
class GFG {
     public static void main(String[] args)
     {
  
         // Get the stream
         Stream<String> stream = Stream.of( "Geeks" , "For" , "Geeks" , "A" , "Computer" , "Portal" );
  
         // Print the stream using peek()
         // by providing a terminal operation count()
         stream.peek(s -> System.out.println(s)).count();
     }
}

输出如下:

Geeks
For
Geeks
A
Computer
Portal

程序2:这种方法不消费流。因此, 以下代码不会引发任何错误。

// Java code to print the elements of Stream
  
import java.util.stream.*;
  
class GFG {
     public static void main(String[] args)
     {
  
         // Get the stream
         Stream<String> stream = Stream.of( "Geeks" , "For" , "srcmini" , "A" , "Computer" , "Portal" );
  
         // Since the stream is not being consumed
         // this will not throw any exception
  
         // Print the stream
         stream.filter(s -> s.startsWith( "G" ))
             .peek(s -> System.out.println( "Filtered value: " + s))
             .map(String::toUpperCase)
             .peek(s -> System.out.println( "Uppercase value :" + s))
             .count();
     }
}

输出如下:

Filtered value: Geeks
Uppercase value :GEEKS
Filtered value: srcmini
Uppercase value :srcmini

赞(0)
未经允许不得转载:srcmini » 如何在Java 8中打印Stream的元素

评论 抢沙发

评论前必须登录!