> 文档中心 > Map集合的基本功能

Map集合的基本功能


Map集合的基本功能

方法名 说明
V put(K,Key,V value) 添加元素
V remove(Object key) 根据键删除键值对元素
void clear() 移除所有键值对元素
boolean containsKey(Object key) 判断集合中是否包含指定的键
boolean containsValue(Object value) 判断集合中是否包含指定的值
boolean isEmpty() 判断集合是否为空
int size() 集合的长度,也就是说键值对的个数

以代码的形式讲解内容

package Demo;import java.util.HashMap;import java.util.Map;public class Demo {    public static void main(String[] args) { //创建Map集合 Map<String, String> s = new HashMap<>(); //V put(K,Key,V value)     添加元素 s.put("iKun","cxk"); s.put("sheep","美羊羊"); s.put("篮球","鸡你太美"); //V remove(Object key)     根据键删除键值对元素 s.remove("篮球"); System.out.println(s.remove("篮球")); //boolean containsKey(Object key)   判断集合中是否包含指定的键 System.out.println(s.containsKey("iKun")); //boolean containsValue(Object value)   判断集合中是否包含指定的值 System.out.println(s.containsValue("cxk")); //boolean isEmpty() 判断集合是否为空 System.out.println(s.isEmpty()); //int size() 集合的长度,也就是说键值对的个数 System.out.println(s.size()); //void clear()      移除所有键值对元素 s.clear();    }}

输出的内容
null
true
true
false
2