Java基础学习笔记之七(3)--Map接口

***Map<K,V>接口

An object that maps keys to values. A map cannot contain
duplicate keys; each key can map to at most one value.
将键映射到值的对象。一个映射不能包含重复的键;每个键最多只能映射一个值。
map中key不能重复,用hashCode进行比较,效果和用equals比较一样,
用hashCode比较会更快一些。

常用方法:

1.Object put(Object key,Object value)
Associates the specified value with the specified key in this map
将指定的值与此映射中的指定键相关联。
关于返回值:如果map中这个key以前不存在,则直接放入,返回一个null,
如果map以前存在这个key,则新value替换旧value,同时返回value.

2.Object get(Object key)
Returns the value to which the specified key is mapped.
返回此映射中映射到指定键的值

3.Object remove(Object key);
Removes the mapping for a key from this map if it is present
如果存在此键的映射关系,则将其从映射中移除

4.boolean containskey(Object key);
Returns true if this map contains a mapping for the specified key
如果此映射包含指定键的映射关系,则返回 true。

5.boolean containsValue(Object value)
Returns true if this map maps one or more keys to the specified value
如果此映射为指定值映射一个或多个键,则返回 true。

6.int size();
Returns the number of key-value mappings in this map
返回此映射中的键-值映射关系数

7.boolean isEmpty()
Returns true if this map contains no key-value mappings.
如果此映射未包含键-值映射关系,则返回 true。

8.void putAll(Map map);
Copies all of the mappings from the specified map to this map
从指定映射中将所有映射关系复制到此映射中(可选操作)。

9.void clear();
Removes all of the mappings from this map.
从此映射中移除所有映射关系
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值