HOWTO · Java

如何在 Java 中列印 HashMap

本帖介紹瞭如何在 Java 中列印 HashMap。

本頁內容

本教程介紹瞭如何在 Java 中列印 HashMap 元素,還列舉了一些示例程式碼來理解這個主題。

HashMap 是 Map 介面的一個實現類,用於將元素收整合鍵和值對。我們可以使用各種方法來列印它的元素。比如 keySet() 方法、values() 方法、entrySet() 方法、asList() 方法等。我們來看一些例子。

Java 中列印 HashMap 元素

這是 Java 中列印 HashMap 最簡單的方法。只要將 HashMap 的引用傳遞到 println() 方法中,它就會將鍵值對列印到大括號中。請看下面的例子。

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);
  }
}

輸出:

{100=Hundred, 1000=Thousand, 10=Ten}

Java 中使用 keySet() 方法列印 HashMap 元素

我們可以使用 keySet() 方法獲得一組鍵,然後在 for 迴圈中使用 get() 方法獲得值。get() 方法返回與傳遞的鍵相關的值。請看下面的例子。

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));
    }
  }
}

輸出:

100 = Hundred
1000 = Thousand
10 = Ten

Java 中使用 forEach() 方法列印 HashMap 元素

從 Java 8 開始,我們可以使用 forEach() 方法,藉助 getKey() 和 getValue() 方法列印 HashMap 元素。getKey() 方法從 entrySet 中返回一個鍵,getValue() 方法返回與該鍵相關的值。請看下面的例子。

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()); });
  }
}

Java 中使用 Arrays.asList() 列印 HashMap 元素

我們可以使用 Arrays.asList() 方法來列印 HashMap 元素。asList() 方法返回 HashMap 的列表形式。請看下面的例子。

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));
  }
}

輸出:

[{100=Hundred, 1000=Thousand, 10=Ten}]

在 Java 中使用 Collections 類列印 HashMap 元素

我們可以使用 Collections.singletonList() 靜態方法來列印 HashMap 元素。singletonList() 方法返回 HashMap 的列表表示。請看下面的例子。

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));
  }
}

輸出:

[{100=Hundred, 1000=Thousand, 10=Ten}]

Java 中使用 entrySet() 方法列印 HashMap 元素

entrySet() 方法返回一組條目,可用於 for 迴圈列印 HashMap 元素。請看下面的例子。

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());
    }
  }
}

輸出:

100 = Hundred
1000 = Thousand
10 = Ten

Java 中使用 values() 和 keySet() 方法列印 HashMap 元素

如果我們想單獨列印值和鍵,我們可以使用 values() 和 keySet() 方法。values() 方法返回所有值的列表,而 keySet() 方法返回所有 HashMap 鍵的列表。請看下面的例子。

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());
  }
}

輸出:

[Hundred, Thousand, Ten]
[100, 1000, 10]

Java 中使用 Biconsumer 列印 HashMap 元素

Biconsumer 是 Java 中的一個介面,可用於使用 lambda 表示式列印 HashMap 元素。請看下面的例子。

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);
  }
}

輸出:

100 = Hundred
1000 = Thousand
10 = Ten