HashMap用法总结
来源:互联网 发布:武汉大学罗知 编辑:程序博客网 时间:2024/05/16 23:55
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:
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();
1 0
- HashMap用法总结
- HashMap用法
- HashMap用法
- HashMap用法
- HashMap用法
- HashMap用法
- HashMap用法
- hashMap用法
- HashMap 用法
- HASHMAP 总结
- HashMap 总结
- HashMap总结
- HashMap总结
- hashmap总结
- HashMap总结
- HashMap用法 示例
- hashmap的用法
- HashMap用法 示例
- B-number(记搜)
- 32 应 用 笔 记
- 低功耗 2
- Html学习1基本的标签(慕课网,Bluefish)
- 多态在 Java 和 C++ 编程语言中的实现比较
- HashMap用法总结
- 图解Java多态内存分配以及多态中成员方法的特点
- UITableView 滑动一下才刷新数据的解决办法
- app提交的注意事项(争取包过)
- 一些json接口
- Codeforces Round #265 (Div. 2) C.No to Palindromes!
- Swift语言简述
- Swift(1)函数
- Swift(2)数组 array