Java集合-Map源码

介绍

Map是一个key到value的映射接口,即map集合存储的是键值对,一个map不能包含重复的key,每个key至多只能映射一个值。Map接口中键和值一一映射. 可以通过键来获取值和设定值。

类图

这里写图片描述

Map的遍历

 // 按照key的字典顺序来排序(升序)
 Map<String, BigDecimal> map = new TreeMap<>();
 for (Map.Entry entry : map.entrySet()) {
     // 遍历map
     TrendDTO dto = new TrendDTO(entry.getKey(),entry.getValue());
     newList.add(dto);
 }

源码

参照JDK1.8版本

查询操作

//返回map的大小
int size();

//map为空,返回true
boolean isEmpty();

//如果map包含指定key,则返回true
boolean containsKey(Object key);

//如果map中有一个或多个key对应的值是指定的value,则返回true
boolean containsValue(Object value);

//按key获取map的指定值
V get(Object key);

修改操作

//按key-value向map中放值
V put(K key, V value);

//删除map中指定的key及对应的value
V remove(Object key);

批量操作

/**
 * Copies all of the mappings from the specified map to this map
 * (optional operation).  
 */
void putAll(Map<? extends K, ? extends V> m);

/**
 * Removes all of the mappings from this map (optional 
 * operation).
 * The map will be empty after this call returns.
 */
void clear();

比较和散列

//Compares the specified object with this map for equality. 
boolean equals(Object o);


//Returns the hash code value for this map. 
int hashCode();

key value null

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值