Try Java 8 Stream

I think I'm doing it somewhere, but I'm going to try the performance when using Stream with Java 8.




$ java -version
java version "1.8.0_111"
Java(TM) SE Runtime Environment (build 1.8.0_111-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)


Version: Neon.1a Release (4.6.1)

Test method


--List of String

things to do

--Sort the list --Loop the list and concatenate with StringBuilder --Exclude if the list string starts with a specific character

Code that does not use Stream

	public void sortList(List<String> list) {
		StringBuilder sb = new StringBuilder();
		for (String str : list) {
			if (!str.startsWith("Pe")) {

Code using Stream

	public void sortList(List<String> list) {
		StringBuilder sb = new StringBuilder();
				.filter(str -> !str.startsWith("Pe"))
				.forEach(str -> sb.append(str));

Code that executes the method

Executes the process of looping the test method 1 million times 10 times.

	public static void main(String[] args) {
		//List to use in test method arguments
		List<String> list = Arrays.asList(
				,"Pepe Pepe"
		//Let's write a test execution loop with Stream
		IntStream.rangeClosed(1, 10).forEach(i -> {
			long start = System.currentTimeMillis();
			StreamTest test = new StreamTest();
			IntStream.rangeClosed(1, 1000000).forEach(j -> {
			System.out.println("[time]" + (System.currentTimeMillis() - start) + "(msec)");


Code that does not use Stream


Code using Stream



In the code I tried this time, it took twice to more than twice as long when using Stream. However, since it is a large number of loops with simple processing, it seems that this result was obtained. (I feel that the same result will not be obtained when it is put into practical use.) Also, since this is the first implementation of Stream, it may not be possible to implement using Stream. Regarding readability, in the case of this test, I think that those who are accustomed to the code so far will obviously find it easier to see using the for statement, Once I got used to it, the syntax using Stream was obvious, and I felt that it was easy to understand.

By the way, the following code made it a little faster.

	public void sortListAsStream(List<String> list) {
		StringBuilder sb = new StringBuilder();
				.forEach(str -> {
					if (!str.startsWith("Pe")) {

Recommended Posts

Try Java 8 Stream
[JAVA] Stream type
Java Stream API
Studying Java 8 (Stream)
Roughly try Java 9
Java Stream termination
[Java] Stream processing
Java 9 Optional :: stream
Try using the Stream API in Java
Try various Java Stream API methods (now)
[Java] Stream Collectors notes
[Java] Stream API-Stream generation
[Java] Stream API / map
Java8 Stream API practice
Java8 Stream reduction operation
Try Java return value
Java8 Stream Rough Summary
Try using RocksDB in Java
Try DB connection with Java
[Java11] Stream Summary -Advantages of Stream-
Try scraping using java [Notes]
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Try functional type in Java! ①
Java Stream API cheat sheet
Try gRPC with Java, Maven
Java Stream API in 5 minutes
Java8 stream, lambda expression summary
[Java] Stream API --Stream termination processing
[Java] Stream API --Stream intermediate processing
Java Stream cannot be reused.
[Java] Introduction to Stream API
Use Redis Stream in Java
[Java11] Stream Usage Summary -Basics-
Java application for beginners: stream
[Java] Stream API intermediate operation
[Java 8] Duplicate deletion (& duplicate check) with Stream
Try implementing Android Hilt in Java
[Java] Stream (filter, map, forEach, reduce)
Try implementing GraphQL server in Java
[java8] To understand the Stream API
About Lambda, Stream, LocalDate of Java8
[Introduction to Java] About Stream API
[Java] Element existence check with Stream
Try running Selenuim 3.141.59 in eclipse (java)
Try using Redis with Java (jar)
Try an If expression in Java
I tried using Java8 Stream API
[Java] Try to implement using generics
Try to extract java public method
Try bidirectional communication with gRPC Java
Basic processing flow of java Stream
Try running AWS X-Ray in Java
Try to implement Yubaba in Java
Try using IBM Java method tracing
Java 8 ~ Stream API ~ to start now
Try Eclipse 4.7 Oxygen New 30+ / Java 10 var!
Java array / list / stream mutual conversion list
Java8 list conversion with Stream map