LongStream count() in Java with examples Last Updated : 26 Mar, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report LongStream count() returns the count of elements in the stream. LongStream count() is present in java.util.stream.LongStream Syntax : long count() Example 1 : Count the elements in LongStream. Java // Java code for LongStream count() // to count the number of elements in // given stream import java.util.*; import java.util.stream.LongStream; class GFG { // Driver code public static void main(String[] args) { // creating a stream LongStream stream = LongStream.of(2L, 3L, 4L, 5L, 6L, 7L, 8L); // storing the count of elements // in a variable named total long total = stream.count(); // displaying the total number of elements System.out.println(total); } } Output: 7 Example 2 : Count the elements in a given range. Java // Java code for LongStream count() // to count the number of elements in // given range excluding the last element import java.util.*; import java.util.stream.LongStream; class GFG { // Driver code public static void main(String[] args) { // creating a stream LongStream stream = LongStream.range(2, 50); // storing the count of elements // in a variable named total long total = stream.count(); // displaying the total number of elements System.out.println(total); } } Output: 48 Example 3 : Count distinct elements in LongStream. Java // Java code for LongStream count() // to count the number of distinct // elements in given stream import java.util.*; import java.util.stream.LongStream; class GFG { // Driver code public static void main(String[] args) { // creating a stream LongStream stream = LongStream.of(2L, 3L, 4L, 4L, 7L, 7L, 8L); // storing the count of distinct elements // in a variable named total long total = stream.distinct().count(); // displaying the total number of elements System.out.println(total); } } Output: 5 Comment More infoAdvertise with us Next Article Java lang.Long.builtcount() method in Java with Examples S Sahil_Bansall Follow Improve Article Tags : Java Practice Tags : Java Similar Reads Stream count() method in Java with examples long count() returns the count of elements in the stream. This is a special case of a reduction (A reduction operation takes a sequence of input elements and combines them into a single summary result by repeated application of a combining operation). This is a terminal operation i.e, it may travers 2 min read IntStream count() in Java with examples IntStream count() returns the count of elements in the stream. IntStream count() is present in java.util.stream.IntStream Syntax : long count() Example 1 : Count the elements in IntStream. Java // Java code for IntStream count() // to count the number of elements in // given stream import java.util. 2 min read Java streams counting() method with examples In Java 8, there is predefined counting() method returned by Collectors class counting() method to count the number of elements in a Stream. Syntax : public static Collector counting() Where, output is a Collector, acting on a Stream of elements of type T, with its finisher returning the âcollectedâ 2 min read Java lang.Long.builtcount() method in Java with Examples java.lang.Long.bitCount() is a built in function in Java that returns the number of set bits in a binary representation of a number. It accepts a single mandatory parameter number whose number of set bits is returned. Syntax: public static long bitCount(long num) Parameters: num - the number passed 3 min read StringBuilder length() in Java with Examples The length() method of StringBuilder class returns the number of character the StringBuilder object contains. The length of the sequence of characters currently represented by this StringBuilder object is returned by this method. Syntax: public int length() Return Value: This method returns length o 2 min read Dictionary size() Method in Java with Examples The size() Method of Dictionary class in Java is used to know the size of the dictionary or the number of distinct keys present in the dictionary. Syntax: DICTIONARY.size() Parameters: The method does not accept any parameters. Return Value: The method returns the number of keys present in the dicti 2 min read Like