首页 > 编程语言 >Java 8 | Collectors counting() with Examples

Java 8 | Collectors counting() with Examples

时间:2022-11-27 03:22:06浏览次数:36  
标签:elements Java stream Collectors Examples input counting method

https://www.geeksforgeeks.org/java-8-collectors-counting-with-examples/

Collectors counting() method is used to count the number of elements passed in the stream as the parameter. It returns a Collector accepting elements of type T that counts the number of input elements. If no elements are present, the result is 0. It is a terminal operation i.e, it may traverse the stream to produce a result or a side-effect. It returns the total count of elements in the stream which reach the collect() method after undergoing various pipelined stream operations such as filtering, reduction etc.

Syntax:

 
public static <T> Collector<T, ?, Long> counting()

where the mentioned terms are as follows:

  • Interface Collector<T, A, R>: A mutable reduction operation that accumulates input elements into a mutable result container, optionally transforming the accumulated result into a final representation after all input elements have been processed. Reduction operations can be performed either sequentially or in parallel.
    • T: The type of input elements to the reduction operation.
    • A: The mutable accumulation type of the reduction operation.
    • R: The result type of the reduction operation.
  • Long: The Long class wraps a value of the primitive type long in an object. An object of type Long contains a single field whose type is long. In addition, this class provides several methods for converting a long to a String and a String to a long, as well as other constants and methods useful when dealing with a long.
  • T: The type of the input elements.

Parameters: This method does not take any parameter.

  <iframe data-google-container-id="1" data-is-safeframe="true" data-load-complete="true" frameborder="0" height="1" id="google_ads_iframe_/27823234/GFG_Desktop_PostContent_336x280_0" marginheight="0" marginwidth="0" scrolling="no" src="https://43ba9ee720d6cc73d017f158ebf35783.safeframe.googlesyndication.com/safeframe/1-0-40/html/container.html" title="3rd party ad content" width="1"></iframe>

Return Value: A Collector that counts the input elements. The count is returned as Long object.

Below are examples to illustrate counting() method:

Program 1:

 
// Java code to show the implementation of // Collectors counting() method    import java.util.stream.Collectors; import java.util.stream.Stream;    class GFG {        // Driver code     public static void main(String[] args)     {         // creating a stream of strings         Stream<String> s = Stream.of("1", "2", "3", "4");            // using Collectors counting() method to         // count the number of input elements         long ans = s.collect(Collectors.counting());            // displaying the required count         System.out.println(ans);     } }
Output:
4

Program 2: When no element is passed as input element.

 
// Java code to show the implementation of // Collectors counting() method    import java.util.stream.Collectors; import java.util.stream.Stream;    class GFG {        // Driver code     public static void main(String[] args)     {         // creating a stream of strings         Stream<String> s = Stream.of();            // using Collectors counting() method to         // count the number of input elements         long ans = s.collect(Collectors.counting());            // displaying the required count         System.out.println(ans);     } }

标签:elements,Java,stream,Collectors,Examples,input,counting,method
From: https://www.cnblogs.com/kungfupanda/p/16928908.html

相关文章

  • Stream In Java
    https://www.geeksforgeeks.org/stream-in-java/ IntroducedinJava8,theStreamAPIisusedtoprocesscollectionsofobjects.Astreamisasequenceofobje......
  • Comparable vs Comparator in Java
    https://www.geeksforgeeks.org/comparable-vs-comparator-in-java/ Javaprovidestwointerfacestosortobjectsusingdatamembersoftheclass:  Comparabl......
  • equals() and hashCode() methods in Java
    https://www.geeksforgeeks.org/equals-hashcode-methods-java/ Java.lang.objecthastwoveryimportantmethodsdefined:publicbooleanequals(Objectobj)andp......
  • enum in Java
    https://www.geeksforgeeks.org/enum-in-java/Enumerationsservethepurposeofrepresentingagroupofnamedconstantsinaprogramminglanguage.Forexample,t......
  • Generics in Java
    https://www.geeksforgeeks.org/generics-in-java/ Generics means parameterizedtypes.Theideaistoallowtype(Integer,String,…etc.,anduser-defined......
  • java发送邮件
    一邮件协议收发邮件具有与HTTP协议相同的邮件传输协议.SMTP:(SimpleMailTransferProtocol,简单邮件传输协议)发邮件协议POP3:(PostOfficeProtocolVersion3,邮局协议第......
  • Java中YYYY-MM-dd在跨年时出现的bug
    先看一张图:Bug的产生原因:日期格式化时候,把yyyy-MM-dd写成了YYYY-MM-dd Bug分析:当时间是2019-08-31时,publicclassDateTest{publicstaticvoidmain(Strin......
  • java中乐观锁CAS的实现探究——AtomicInteger
    CASCAS——compareandswap,比较并交换。是一种乐观锁的实现方式。更新操作时,在每次更新之前,都先比较一下被更新的目标T,值是不是等于读取到的旧值O,如果相等,则认为在读取......
  • JAVA网络编程TCP实现聊天功能,附在IDEA中同时运行2个或以上相同的java程序
    在IDEA中同时运行2个或以上相同的java程序在日常编写测试代码时,有时候会需要在idea上同时运行两个及以上相同的java程序,如:想运行两个CLIENTLOGIN测试聊天室效果。1.点击E......
  • java9
    Java9新特性Java9发布于2017年9月22日,带来了很多新特性,其中最主要的变化是已经实现的模块化系统。接下来我们会详细介绍Java9的新特性。Java9新特性模块系......