Map
Map hashMap = new HashMap();
// V put(Key,Value); 添加
hashMap.put(1,"一");
hashMap.put(2,"二");
hashMap.put(3,"三");
hashMap.put(4,"四");
// V get(Key); 通过Key取值
System.out.println( hashMap.get(1));;
// void Clear(); 清空集合
// boolean containsKey(Object key); 判断集合包含某个Key
System.out.println(hashMap.containsKey(1));
// boolean containsValue(Object value); 判断集合包含某个Value
System.out.println(hashMap.containsValue("一"));
// boolean isEmpty(); 判断集合是否有元素
System.out.println(hashMap.isEmpty());
// Set<K> keySet(); 获取Map集合中所有Key (所有Key都是set集合)
Set set = hashMap.keySet();
// V remove(Object key); 通过键删除键值对;
hashMap.remove(4);
// int size(); 获取集合中元素集合
hashMap.size();
//Colletion<V> values(); 获取Map集合中所有vlaue,返回一个Colletion
Collection ct = hashMap.values();
//Set<Map.Entry<K,V>> entrySet();
// Entry是Map中的静态内部类
//转换Set集合 每个元素类型MAp.Entry 如下
// 1=一
// 2=二
// 3=三
//遍历Map集合
//keyset返回Set集合
Set<Integer> keys = hashMap.keySet();
//创建迭代器
Iterator<Integer> key = keys.iterator();
while (key.hasNext()){
//遍历key,用get方法利用key取value
System.out.println(hashMap.get(key.next()));
}
//遍历Map集合
//转换成Set集合,元素类型为Map.Entry
Set<Map.Entry> set1 = hashMap.entrySet();
//创建迭代器遍历
Iterator<Map.Entry> it = set1.iterator();
while (it.hasNext()){
Map.Entry node = it.next();
//取key
System.out.println(node.getKey());
//取value
System.out.println(node.getValue());
//不操作,key=value
System.out.println(node);
}
//或者
for (Map.Entry node:set1
) {
System.out.println(node);//建议使用这种方法遍历,效率高
}