Il metodo Java HashMap size () restituisce il numero di mappature chiave / valore presenti nella hashmap.
La sintassi del size()
metodo è:
hashmap.size()
In questo caso, hashmap è un oggetto della HashMap
classe.
size () Parametri
Il size()
metodo non accetta parametri.
size () Valore restituito
- restituisce il numero di mappature chiave / valore presenti nella hashmap
Esempio: dimensione Java HashMap ()
import java.util.HashMap; class Main ( public static void main(String() args) ( // create an HashMap HashMap countries = new HashMap(); // insert keys/values to the HashMap countries.put("USA", "Washington"); countries.put("UK", "London"); countries.put("Canada", "Ottawa"); System.out.println("HashMap: " + countries); // get the number of keys/values from HashMap int size = countries.size(); System.out.println("Size of HashMap: " + size); ) )
Produzione
HashMap: (Canada = Ottawa, USA = Washington, UK = Londra) Dimensioni di HashMap: 3
Nell'esempio sopra, abbiamo creato una hashmap denominata paesi. Qui abbiamo usato il size()
metodo per ottenere il numero di mappature chiave / valore presenti nella hashmap.