中小企业网站建设公司首选,lamp网站开发黄金组...,邯郸教育行业网站建设,wordpress pdf 显示不了Map集合的常用方法 方法名称作用V put(Key k,V value)添加元素V remove(K key, V value)根据键值删除对应的值void clear()清除所有键值元素boolean containsKey(Object key)判断集合中是否包含指定的键boolean containsValue(Object value)判断集合中是否包含指定的值boolean …Map集合的常用方法 方法名称作用V put(Key k,V value)添加元素V remove(K key, V value)根据键值删除对应的值void clear()清除所有键值元素boolean containsKey(Object key)判断集合中是否包含指定的键boolean containsValue(Object value)判断集合中是否包含指定的值boolean isEmpty()判断集合是否为空size()返回集合中存放元素的个数 示例代码
package com.collection.Demo09;import java.util.HashMap;
import java.util.Map;public class Test01 {public static void main(String[] args) {MapString, String hashMap new HashMap();System.out.println(put);hashMap.put(mayikt001, 小明);hashMap.put(mayikt002, xiaojun);hashMap.put(mayikt003, xiaoli);hashMap.put(mayikt003, 小王); //键是不允许重复的这里并不会报错而是修改K003的值为VmayiktSystem.out.println(hashMap);//{mayikt002xiaojun, mayikt001小明, mayikt003小王}//注意上面遍历的顺序并不是put插入的顺序——∴元素存取是散列无序的System.out.println(remove);hashMap.remove(mayikt001); //返回String类型
// hashMap.remove(mayikt002,xiaojun);//只有键值对 都 存在才可以删除返回Boolean类型hashMap.remove(xiaojun); //没有这个键 执行并不报错System.out.println(hashMap);System.out.println(clean);
// hashMap.clear();//清空hashMap集合中所有键值对System.out.println(hashMap);//{}System.out.println(containsKey);//判断在 hashMap集合中 是否存在 键值mayikt002, 返回true/falseSystem.out.println(hashMap.containsKey(mayikt002));//trueSystem.out.println(containsValue);System.out.println(hashMap.containsValue(xiaowang));//falseSystem.out.println(hashMap.containsValue(小王));//trueSystem.out.println(hashMap.containsValue(小明));//falseSystem.out.println(isEmpty);System.out.println(hashMap.isEmpty());//falseHashMapString, String hashMap1 new HashMap();System.out.println(hashMap1.size());//0 hashMap1中元素的个数HashMapString, String hashMap2 null;System.out.println(hashMap1.isEmpty());//true
// System.out.println(hashMap2.isEmpty());//报错.NullPointerException}
}下一篇文章