public static void main(String[] args) {
HashMap hashMap = new HashMap();
hashMap.put("aa", 123);//添加"aa"
hashMap.put("bb", 345);
hashMap.put("cc", 89);
hashMap.put("aa", 890);//修改"aa"
System.out.println(hashMap);
HashMap hashMap1 = new HashMap();
hashMap1.put("AA", 123);
hashMap1.put("BB", 456);
hashMap.putAll(hashMap1);//添加另一个map中全部的元素
System.out.println(hashMap);
//遍历所有的key集
Set set = hashMap.keySet();
Iterator iterator = set.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
//遍历所有的value集
Collection values = hashMap.values();
Iterator iterator1 = values.iterator();
while (iterator1.hasNext()){
System.out.println(iterator1.next());
}
//遍历所有key-value
Set entrySet = hashMap.entrySet();
Iterator iterator2 = entrySet.iterator();
while (iterator2.hasNext()){
Object obj = iterator2.next();
Map.Entry entry = (Map.Entry) obj;
System.out.println(entry.getKey()+"="+entry.getValue());
}
Object value = hashMap.remove("AA");//不存在返回null
System.out.println(value);
System.out.println(hashMap);
System.out.println(hashMap.get("BB"));//不存在返回null
boolean containsKey = hashMap.containsKey("BB");//判断map中是否存在Key
boolean containsValue = hashMap.containsValue(456);//判断map中是否存在value
System.out.println(containsKey);
System.out.println(containsValue);
hashMap.clear();//将map清空,不是变为null
System.out.println(hashMap.size());
}
JAVA:集合Map(二)
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。