從 Java 中的 HashMap 中提取值


若要從 HashMap 中提取值,首先讓我們建立一個帶有鍵和值的 HashMap −

HashMap<Integer, Integer>m = new HashMap<Integer, Integer>();

現在,向 HashMap 中新增一些元素 −

m.put(10, 20);
m.put(30, 40);
m.put(50, 60);
m.put(70, 80);
m.put(90, 100);
m.put(110, 120);
m.put(130, 140);
m.put(150, 160);

現在,從 HashMap 中提取值 −

for (Integer i: m.keySet()) {
   System.out.println(m.get(i));
}

示例

 即時演示

import java.util.HashMap;
public class Demo {
   public static void main(String args[]) {
      HashMap<Integer, Integer>m = new HashMap<Integer, Integer>();
      m.put(10, 20);
      m.put(30, 40);
      m.put(50, 60);
      m.put(70, 80);
      m.put(90, 100);
      m.put(110, 120);
      m.put(130, 140);
      m.put(150, 160);
      System.out.println("Displaying values from HashMap...");
      for (Integer i: m.keySet()) {
         System.out.println(m.get(i));
      }
   }
}

輸出

Displaying values from HashMap...
60
140
80
160
20
100
40
120

更新日期:2019 年 7 月 30 日

1 千次以上瀏覽

Kickstart Your Career

透過完成課程獲得認證

開始
廣告
© . All rights reserved.