从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
热门推荐
10 生日祝福语 简短独特高考
11 写给情侣祝福语 浪漫简短
12 对离岗同事祝福语简短
13 最美婆婆生日祝福语简短
14 女神节日简短祝福语大全
15 优秀男士祝福语大全简短
16 幼教专干调动祝福语简短
17 特别的端午祝福语简短
18 写给男生简短的祝福语