IntStream count() devuelve el recuento de elementos en la secuencia. IntStream count() está presente en java.util.stream.IntStream
Sintaxis:
long count()
Ejemplo 1: Cuente los elementos en IntStream.
// Java code for IntStream count() // to count the number of elements in // given stream import java.util.*; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // creating a stream IntStream stream = IntStream.of(2, 3, 4, 5, 6, 7, 8); // 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 IntStream count() // to count the number of elements in // given range excluding the last element import java.util.*; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // creating a stream IntStream stream = IntStream.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: Contar elementos distintos en IntStream.
// Java code for IntStream count() // to count the number of distinct // elements in given stream import java.util.*; import java.util.stream.IntStream; class GFG { // Driver code public static void main(String[] args) { // creating a stream IntStream stream = IntStream.of(2, 3, 4, 4, 7, 7, 8); // 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