LongStream count() devuelve el recuento de elementos en la secuencia. LongStream count() está presente en java.util.stream.LongStream
Sintaxis:
long count()
Ejemplo 1: Cuente los elementos en LongStream.
// 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); } }
Producción:
7
Ejemplo 2: Contar los elementos en un rango dado.
// 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); } }
Producción:
48
Ejemplo 3: Cuente elementos distintos en LongStream.
// 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); } }
Producción:
5
Publicación traducida automáticamente
Artículo escrito por Sahil_Bansall y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA