增:
V put(K key, V vlaue);添加键值对的数据到map集合中
void putAll(Map<? extends K> k, Map<? extends V> v);将一个map集合存放到另外一个map集合中
删:
V remove (K key);通过键删除键值对的数据,返回值是值
改:
V put(K key V value);当键值存在的时候,会将value值覆盖掉的
查:
int size(); 查看集合中元素的个数
boolean isEmpty();判断是否为空,如果不为空返回的是false
boolean containsKey();是否包含这个键
boolean containsValue();是否包含这个值
重要的方法:
V get(K key);通过键获取值
Set<K> keySet();获取map集合中的键,然后存到set集合中
Collection<V> values(); 获取map集合中值,存到了Collection集合中
Set<Map.Entry<K,V>> entrySet();将map集合的键值对,存到了set集合
Map.Entry这个接口的方法
getKey:返回键值对的键
getValue:返回键值对
public class Test8 {
public static void main(String[] args) {
Map<Integer,String> map = new HashMap<>();
map.put(1,"张三");
map.put(2,"李四");
System.out.println(map);//{1=张三, 2=李四}
map.put(1,"赵四");//替换
System.out.println(map);//{1=赵四, 2=李四}
Map<Integer,String> map1 = new HashMap<>();
map1.put(3,"王五");
map1.put(4,"李白");
map.putAll(map1);//添加一个map集合
System.out.println(map);//{1=赵四, 2=李四, 3=王五, 4=李白}
System.out.println(map.size());//4,集合中元素个数
System.out.println(map.containsKey(2));//true,集合中是否有这个键
System.out.println(map.containsValue("李白"));//true,集合中是否有这个值
Set<Integer> set = map.keySet();
System.out.println(set);//map集合中所有键存到set集合中
Collection<String> values = map.values();
System.out.println(map.values());//map集合中所有值存到Collction集合中
Set<Map.Entry<Integer,String>> entries = map.entrySet();//map集合中所有键值对存到set集合中
System.out.println(entries);
for (Map.Entry<Integer, String> entry : entries) {
System.out.println(entry);
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}
}
的值