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.

environment

■Java

java


$ 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)

■Eclisep

Version: Neon.1a Release (4.6.1)

Test method

argument

--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();
		Collections.sort(list);
		for (String str : list) {
			if (!str.startsWith("Pe")) {
				sb.append(str);
			}
		}
	}

Code using Stream

	public void sortList(List<String> list) {
		StringBuilder sb = new StringBuilder();
		list.stream()
				.filter(str -> !str.startsWith("Pe"))
				.sorted()
				.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(
				"Yute"
				,"Miyaou"
				,"Kimuko"
				,"1234567"
				,"abcdefg"
				,"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 -> {
				test.sortList(list);
			});
			System.out.println("[time]" + (System.currentTimeMillis() - start) + "(msec)");
		});
	}

result

Code that does not use Stream

[time]174(msec)
[time]144(msec)
[time]161(msec)
[time]117(msec)
[time]136(msec)
[time]204(msec)
[time]132(msec)
[time]133(msec)
[time]126(msec)
[time]127(msec)

Code using Stream

[time]526(msec)
[time]442(msec)
[time]684(msec)
[time]373(msec)
[time]378(msec)
[time]335(msec)
[time]363(msec)
[time]364(msec)
[time]390(msec)
[time]353(msec)

Conclusion

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();
		list.stream()
				.sorted()
				.forEach(str -> {
					if (!str.startsWith("Pe")) {
						sb.append(str);
					}
				});
	}
[time]456(msec)
[time]409(msec)
[time]328(msec)
[time]552(msec)
[time]316(msec)
[time]257(msec)
[time]272(msec)
[time]289(msec)
[time]305(msec)
[time]306(msec)

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
[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
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