在 Java 中顯示 HashMap 的內容


讓我們首先建立一個 HashMap 並新增元素 -

HashMap hm = new HashMap();
hm.put("Wallet", new Integer(700));
hm.put("Belt", new Integer(600));

要顯示內容,只需列印 HashMap 物件 -

System.out.println("Map = "+hm);

以下示例顯示如何顯示 HashMap 的內容 -

示例

 即時演示

import java.util.*;
public class Demo {
   public static void main(String args[]) {
      // Create hash map
      HashMap hm = new HashMap();
      hm.put("Wallet", new Integer(700));
      hm.put("Belt", new Integer(600));
      System.out.println("Map = "+hm);
   }
}

輸出

Map = {Belt=600, Wallet=700}

更新於: 30-Jul-2019

331 次檢視

開啟職業生涯

透過完成課程,獲得認證

開始學習
廣告
© . All rights reserved.