Map<K,V>泛型接口
将键映射到值对象,键不允许重复,每个键最多只有一个值
HashMap方法:
1.put(K key,V value)将数据添加到HashMap
2.public V get(K Key)返回键所对应的值
3.public boolean containsKey(K key)判断键是否存在
4.pblic boolean containsValue(V value)判断值是否存在
5.public Set<K> keySet();返回所有键
6.pblic Collection<V> values()返回所有值
public static void main(String[] args) {
// TODO Auto-generated method stub
Map<Integer,String> map=null;
map=new HashMap<Integer,String>();
map.put(211306435, "a");
map.put(211306420, "b");
map.put(211306422,"c");
map.put(211306418, "d");
String s=map.get(211306435);
System.out.println(s);
if(map.containsKey(211306435))
System.out.println("211306435所对应的值为:"+map.get(211306435));//map.get(211306435).返回211306435这个键的值
else
System.out.println("键不存在");
if(map.containsValue("a"))
System.out.println("map.containsValue(\"a\")的值存在");
else
System.out.println("map.containsValue(\"a\")的值不存在");
Collection<String> collection=map.values();
Iterator<String> i1=collection.iterator();
while(i1.hasNext())
{
String S=i1.next();
if("d".equals(S))
i1.remove();
else
System.out.println(S);
}
Set<Integer> set=map.keySet();
Iterator<Integer> i2=set.iterator();
while(i2.hasNext())
System.out.println(i2.next());
}
}