HashMap extends AbstractMap implements Map

A:

public static void printMap1(Map<String, String> map) {
        System.out.println("\n Method 1");
        Set<Map.Entry<String, String>> entries = map.entrySet();
        for (Map.Entry<String, String> entry : entries) {
            System.out.println(entry.getKey() + ""+ entry.getValue()); }}Copy the code

Member variables marked with the TRANSIENT keyword do not participate in the serialization process.

Map. EntrySet () the source code:

 public Set<Map.Entry<K,V>> entrySet() {
        Set<Map.Entry<K,V>> es;
        return (es = entrySet) == null ? (entrySet = new EntrySet()) : es;
    }
Copy the code

So this is an EntrySet

EntrySet is a collection of key-value pairs in Java, a member class of HashMap called entrySet

final class EntrySet extends AbstractSet<Map.Entry<K.V>> {}
Copy the code

Entry is an inner class in a Map

interface Entry<K.V> {       
        K getKey(a);   
        V getValue(a);
}
Copy the code

Way 2

public static void printMap2(Map<String, String> map) {
        System.out.println("\n Method 2");
        map.forEach((key, value) -> {
            System.out.println(key + "" + value);
        });
    }
Copy the code

Methods three

public static void printMap3(Map<String, String> map) {
        System.out.println("\n Method 3");
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            System.out.println(key + ""+ map.get(key)); }}Copy the code

Methods four

 public static void printMap4(Map<String, String> map) {
        System.out.println("\n Method 4");
        Set<Map.Entry<String, String>> entries = map.entrySet();
        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, String> next = iterator.next();
            System.out.println(next.getKey() + ""+ next.getValue()); }}Copy the code

Methods five

public static void printMap5(Map<String, String> map) {
        System.out.println("\n Method 5");
        Collection<String> values = map.values();
        for(String value : values) { System.out.println(value); }}Copy the code