HashMap用法总结

12 篇文章 0 订阅
5 篇文章 0 订阅

Java中的HashMap的格式为<Key, Value>

和hashtable相比是unsynchronized的,同时也允许null值


常用method:

void clear()
Removes all of the mappings from this map.

boolean containsKey(Object key)
Returns  true if this map contains a mapping for the specified key.
boolean containsValue(Object value)
Returns  true if this map maps one or more keys to the specified value.
V get(Object key)
Returns the value to which the specified key is mapped, or  null if this map contains no mapping for the key.
boolean isEmpty()
Returns  tr
V put(K key, V value)
Associates the specified value with the specified key in this map.
V remove(Object key)
Removes the mapping for the specified key from this map if present.
boolean remove(Object key, Object value)
Removes the entry for the specified key only if it is currently mapped to the specified value.

V replace(K key, V value)
Replaces the entry for the specified key only if it is currently mapped to some value.
boolean replace(K key, V oldValue, V newValue)
Replaces the entry for the specified key only if currently mapped to the specified value.
int size()
Returns the number of key-value mappings in this map.
Set<Map.Entry<K,V>> entrySet()
Returns a  Set view of the mappings contained in this map.

需要注意的是各种输入输出的类型(type),比如get方法返回的直接就是value的type。

关于Map.entry:

Modifier and Type Method and Description
boolean equals(Object o)
Compares the specified object with this entry for equality.
K getKey()
Returns the key corresponding to this entry.
V getValue()
Returns the value corresponding to this entry.
int hashCode()
Returns the hash code value for this map entry.
V setValue(V value)
Replaces the value corresponding to this entry with the specified value (optional operation).
关于set:

Iterator<E> iterator()
Returns an iterator over the elements in this set.

关于Iterator:

boolean hasNext()
Returns  true if the iteration has more elements.
E next()
Returns the next element in the iteration.
void remove()
Removes from the underlying collection the last element returned by this iterator (optional operation).

例:以下为某次写的一小段代码,对于返回值类型为object还是key,value本身的类型非常迷茫,于是有很多冗余的cast:

Iterator iterator = map.entrySet().iterator();
   
    while (iterator.hasNext()) {
      Map.Entry pairs = (Map.Entry)iterator.next();
      if (Integer.parseInt(pairs.getValue().toString()) % 2 == 1) {
        iterator.remove();
        return pairs.getKey().toString();
      }
    }

我用的HashMap<String, Integer>

其中pairs.getValue()已经是所需的东西了,并不需要用
Integer.parseInt(pairs.getValue().toString())
如此复杂的东西来表示~ 同理pairs.getKey()也不需要pairs.getKey().toString();

HashMap常用方法的使用包括: 1. put(K key, V value):将键(key)/值(value)映射存放到Map集合中。通过使用put方法,可以将指定的键值对添加到HashMap中。 2. get(Object key):根据键获取相应的值。通过使用get方法,可以根据键来获取对应的值。如果键不存在,则返回null。 3. remove(Object key):根据键移除相应的键值对。通过使用remove方法,可以根据键来从HashMap中移除对应的键值对。 4. containsKey(Object key):判断HashMap是否包含指定的键。通过使用containsKey方法,可以判断HashMap中是否存在指定的键。如果存在,则返回true;否则返回false。 5. containsValue(Object value):判断HashMap是否包含指定的值。通过使用containsValue方法,可以判断HashMap中是否存在指定的值。如果存在,则返回true;否则返回false。 6. keySet():返回HashMap中所有键的集合。通过使用keySet方法,可以获取HashMap中所有键的集合,以便进行遍历或其他操作。 7. values():返回HashMap中所有值的集合。通过使用values方法,可以获取HashMap中所有值的集合,以便进行遍历或其他操作。 8. entrySet():返回HashMap中所有键值对的集合。通过使用entrySet方法,可以获取HashMap中所有键值对的集合,以便进行遍历或其他操作。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [hashmap常用方法](https://blog.csdn.net/qq_36833673/article/details/123937011)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [HashMap常用方法总结](https://blog.csdn.net/weixin_46224056/article/details/130726098)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [基于HashMap遍历和使用方法(详解)](https://download.csdn.net/download/weixin_38672739/12775309)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值