Java 程序以从 HashMap 中检索所有值集
首先,创建一个 HashMap 并添加元素 −
HashMap hm = new HashMap();
hm.put("Wallet", new Integer(700));
hm.put("Belt", new Integer(600));
hm.put("Backpack", new Integer(1200));现在,检索所有值 −
Collection getValues = hm.values();
System.out.println("
Values...");
Iterator i = getValues.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}以下是一个获取 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));
hm.put("Backpack", new Integer(1200));
System.out.println("Map = "+hm);
Collection getValues = hm.values();
System.out.println("
Values...");
Iterator i = getValues.iterator();
while (i.hasNext()) {
System.out.println(i.next());
}
}
}输出
Map = {Backpack=1200, Belt=600, Wallet=700}
Values...
1200
600
700
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP