Java 8 Stream The peek() is not working with count()?

摘要: Many examples are using the .count() as the terminal operation for .peek(), for example:

Many examples are using the .count() as the terminal operation for .peek(), for example:

Java 8
	List<String> l = Arrays.asList("A", "B", "C", "D");
	long count = l.stream().peek(System.out::println).count();
	System.out.println(count); // 4

Output – It’s working fine.


However, for Java 9 and above, the peek() may print nothing:

Java 9 and above
	List<String> l = Arrays.asList("A", "B", "C", "D");
	long count = l.stream().peek(System.out::println).count();
	System.out.println(count); // 4

Output


Why peek() print nothing now?

Refer to the Java 9 .count() Java docs

An implementation may choose to not execute the stream pipeline (either sequentially or in parallel) 
if it is capable of computing the count directly from the stream source. 
In such cases no source elements will be traversed and no intermediate operations will be evaluated. 

Since Java 9, if JDK compiler is able computing the count directly from the stream (optimization in Java 9), it didn’t traverse the stream, so there is no need to run peek() at all.

	List<String> l = Arrays.asList("A", "B", "C", "D");
	// JDK compiler know the size of the stream via the variable l 
	long count = l.stream().peek(System.out::println).count();

To force the peek() to run, just alter some elements with filter() or switch to another terminal operation like collect()

filter()
	List<String> l = Arrays.asList("A", "B", "C", "D");
	long count = l.stream()
			.filter(x->!x.isEmpty())
			.peek(System.out::println)
			.count();
	System.out.println(count); // 4

Output


collect()
	List<String> l = Arrays.asList("A", "B", "C", "D");
	List<String> result = l.stream()
			.peek(System.out::println)
			.collect(Collectors.toList());
	System.out.println(result.size()); // 4

Output


Be careful of mixing .peek() with .count(), the peek() may not work as expected in Java 9 and above.

P.S Tested with Java 12

上一篇: Java 8 Convert ZonedDateTime to Timestamp
下一篇: Java 8 Convert LocalDateTime to Timestamp
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号