一、方法:keySet()
1、现实意义:
在Hashmap中遍历字段
2、代码demo:
public class HashMapTest
{
public static void main(String[] args)
{
HashMap hashMap = new HashMap();
hashMap.put("a", "zhang");
hashMap.put("b", "zhang");
hashMap.put("c", "zhng");
Set set = hashMap.keySet();//*执行处
for(Iterator iterator = set.iterator();iterator.hasNext();)
{
String s1 = (String)iterator.next();
String s2 = (String)hashMap.get(s1);
System.out.println(s1+":"+s2);
}
}
}
二、方法:entryset()
1、现实意义:
也是进行遍历,不过效率比keySet()
要高
2、代码demo:
public class hashMapTest
{
public static void main(String[] args)
{
HashMapmap = new HashMap();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
}
}
}
三、方法:containsKey()
1、现实意义:
containsKey(Object key) 方法是用来检查此映射是否包含指定键的映射关系。
也可以说明,这个Key值,在hashMap的数据里是否有对应的字符。
2、代码demo:
/****
声明:
以下是java.util.HashMap.containsKey()方法的声明。
public boolean containsKey(Object key);
参数:
key--这是其是否存在于此映射是要测试的键。
返回值:
该方法调用返回true,如果此映射包含指定键的映射关系。
异常:
NA
****/
package com.yiibai;
import java.util.*;
public class HashMapDemo {
public static void main(String args[]) {
// create hash map
HashMap newmap = new HashMap();
// populate hash map
newmap.put(1, "tutorials");
newmap.put(2, "point");
newmap.put(3, "is best");
// check existence of key 2
System.out.println("Check if key 2 exists: " + newmap.containsKey(2));
}
}
/*
结果:
Check if key 2 exists: true
*/
3、个人体会:
就是查询彼此之间的值嘛。