how to print out a map java code example
Example 1: print map java
map.forEach((key, value) -> System.out.println(key + ":" + value));
Example 2: java map print
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (Integer key : map.keySet()) {
System.out.println("Key = " + key);
}
for (Integer value : map.values()) {
System.out.println("Value = " + value);
}
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
Iterator<Map.Entry<Integer, Integer>> entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<Integer, Integer> entry = entries.next();
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
Map map = new HashMap();
Iterator entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
Integer key = (Integer)entry.getKey();
Integer value = (Integer)entry.getValue();
System.out.println("Key = " + key + ", Value = " + value);
}
Example 3: how to print the map in java
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
class MapUtil
{
public static void main (String[] args)
{
Map<Integer, String> map = new HashMap<>();
map.put(1, "One");
map.put(2, "Two");
Iterator<Integer> itr = map.keySet().iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
for (Integer key : map.keySet()) {
System.out.println(key);
}
map.keySet().iterator()
.forEachRemaining(System.out::println);
map.keySet().stream()
.forEach(System.out::println);
Stream.of(map.keySet().toArray())
.forEach(System.out::println);
System.out.println(map.keySet().toString());
Stream.of(map.keySet().toString())
.forEach(System.out::println);
}
}
Example 4: print map in java
Map<String, Integer> map = new HashMap<>();
map.put("a", 1);
map.put("b", 2);
System.out.println(Arrays.asList(map));
System.out.println(Collections.singletonList(map));