Método SortedSet size() en Java con ejemplos

El método size() de la interfaz SortedSet se utiliza para obtener el tamaño del SortedSet o el número de elementos presentes en el SortedSet.

Sintaxis:

int size()

Parámetros: Este método no toma ningún parámetro.

Valor devuelto: el método devuelve el tamaño o la cantidad de elementos presentes en SortedSet.

Nota : El método size() en SortedSet se hereda de la interfaz Set en Java.

El siguiente programa ilustra el método java.util.Set.size():

// Java code to illustrate Set.size() method
  
import java.util.*;
  
public class SetDemo {
    public static void main(String args[])
    {
  
        // Creating an empty Set
        SortedSet<String> set
            = new TreeSet<String>();
  
        // Use add() method to
        // add elements into the Set
        set.add("Welcome");
        set.add("To");
        set.add("Geeks");
        set.add("4");
        set.add("Geeks");
  
        // Displaying the Set
        System.out.println("Set: " + set);
  
        // Displaying the size of the Set
        System.out.println(
            "The size of the SortedSet is: "
            + set.size());
    }
}
Producción:

Set: [4, Geeks, To, Welcome]
The size of the SortedSet is: 4

Referencia : https://docs.oracle.com/javase/7/docs/api/java/util/Set.html#size()

Publicación traducida automáticamente

Artículo escrito por gopaldave y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *