哈希表-java

import java.util.HashMap;
import java.util.Iterator; public class JavaHashMap { public static void main(String[] args) { //定义HashMap
HashMap<String, String> hm = new HashMap<String, String>();
//添加字典
hm.put("username", "password");
hm.put("Jim", "1155689");
hm.put("Jane", "1255669");
hm.put("Kevin", "1165669"); //测试 key 是否包含 username,返回值为 true/false
System.out.println(hm.containsKey("username"));
System.out.println("===================>"); //获取 key 所对应的 vlaue
System.out.println(hm.get("Jim"));
System.out.println("===================>"); //获取整个字典数据
System.out.println(hm.entrySet());
System.out.println("===================>"); //循环打印每一对 key:value
Iterator<?> it = hm.entrySet().iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
System.out.println("===================>"); //分别获取 key 的值, 和 value 的值。
Iterator<String> it2 = hm.keySet().iterator();
while (it2.hasNext()) {
//获得字典的 key(username)
String username = (String) it2.next();
System.out.println(username);
//获得字典的 value(节点)
String password = hm.get(username);
System.out.println(password);
}
}
}

  

上一篇:cf499B-Lecture 【map】


下一篇:Theano入门神经网络(一)