Stampa HashMap in Java
-
Stampa elementi
HashMapin Java -
Stampa gli elementi
HashMaputilizzando il metodokeySet()in Java -
Stampa gli elementi
HashMapusando il metodoforEach()in Java -
Stampa elementi
HashMapusandoArrays.asList()in Java -
Stampa gli elementi
HashMaputilizzando la classeCollectionsin Java -
Stampa gli elementi
HashMaputilizzando il metodoentrySet()in Java -
Stampa gli elementi
HashMapusando il metodovalues()ekeySet()in Java -
Stampa elementi
HashMapusandoBiconsumerin Java
Questo tutorial introduce come stampare gli elementi HashMap in Java.
HashMap è una classe di implementazione dell’interfaccia Map che viene utilizzata per raccogliere elementi in coppie chiave e valore. Possiamo usare vari metodi per stampare i suoi elementi. Ad esempio, il metodo keySet(), il metodo values(), il metodo entrySet(), il metodo asList(), ecc. Vediamo alcuni esempi.
Stampa elementi HashMap in Java
Questo è il modo più semplice per stampare HashMap in Java. Basta passare il riferimento di HashMap al metodo println() e stamperà le coppie chiave-valore tra le parentesi graffe. Vedi l’esempio sotto.
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
System.out.println(map);
}
}
Produzione:
{100=Hundred, 1000=Thousand, 10=Ten}
Stampa gli elementi HashMap utilizzando il metodo keySet() in Java
Possiamo usare il metodo keySet() per ottenere un set di chiavi e quindi ottenere il valore usando il metodo get() nel cicli for. Il metodo get() restituisce il valore associato alla chiave passata. Vedi l’esempio sotto.
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
for (Integer key : map.keySet()) {
System.out.println(key + " = " + map.get(key));
}
}
}
Produzione:
100 = Hundred
1000 = Thousand
10 = Ten
Stampa gli elementi HashMap usando il metodo forEach() in Java
Da Java 8, possiamo usare il metodo forEach() per stampare gli elementi HashMap con l’aiuto dei metodi getKey() e getValue(). Il metodo getKey() restituisce una chiave da entrySet e il metodo getValue() restituisce il valore associato alla chiave. Vedi l’esempio sotto.
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
map.entrySet().forEach(
entry -> { System.out.println(entry.getKey() + " = " + entry.getValue()); });
}
}
Stampa elementi HashMap usando Arrays.asList() in Java
Possiamo usare il metodo Arrays.asList() per stampare gli elementi HashMap. Il metodo asList() restituisce una rappresentazione in lista di HashMap. Vedi l’esempio sotto.
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
System.out.println(Arrays.asList(map));
}
}
Produzione:
[{100=Hundred, 1000=Thousand, 10=Ten}]
Stampa gli elementi HashMap utilizzando la classe Collections in Java
Possiamo usare il metodo statico Collections.singletonList() per stampare gli elementi HashMap. Il metodo singletonList() restituisce una rappresentazione in lista di HashMap. Vedi l’esempio sotto.
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
System.out.println(Collections.singletonList(map));
}
}
Produzione:
[{100=Hundred, 1000=Thousand, 10=Ten}]
Stampa gli elementi HashMap utilizzando il metodo entrySet() in Java
Il metodo entrySet() restituisce un insieme di voci che possono essere usate nel cicli for per stampare elementi HashMap. Vedi l’esempio sotto.
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
for (Map.Entry<Integer, String> entry : map.entrySet()) {
System.out.println(entry.getKey() + " = " + entry.getValue());
}
}
}
Produzione:
100 = Hundred
1000 = Thousand
10 = Ten
Stampa gli elementi HashMap usando il metodo values() e keySet() in Java
Se vogliamo stampare valori e chiavi indipendentemente, possiamo usare i metodi values() e keySet(). Il metodo values() restituisce una lista di tutti i valori, mentre il metodo keySet() restituisce una lista di tutte le chiavi HashMap. Vedi l’esempio sotto.
import java.util.HashMap;
import java.util.Map;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
System.out.println(map.values());
System.out.println(map.keySet());
}
}
Produzione:
[Hundred, Thousand, Ten]
[100, 1000, 10]
Stampa elementi HashMap usando Biconsumer in Java
Il Biconsumer è un’interfaccia in Java che può essere usata per stampare elementi HashMap usando l’espressione lambda. Vedi l’esempio sotto.
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
public class SimpleTesting {
public static void main(String[] args) {
Map<Integer, String> map = new HashMap<>();
map.put(10, "Ten");
map.put(100, "Hundred");
map.put(1000, "Thousand");
BiConsumer<Integer, String> biconsumer = (key, val) -> System.out.println(key + " = " + val);
map.forEach(biconsumer);
}
}
Produzione:
100 = Hundred
1000 = Thousand
10 = Ten