HashMap用法总结

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();

时间: 2024-08-06 22:13:22

HashMap用法总结的相关文章

Java之HashMap用法

源码: 1 package test_demo; 2 3 import java.util.HashMap; 4 import java.util.Iterator; 5 import java.util.Map; 6 import java.util.Random; 7 8 /* 9 * @desc HashMap测试程序 10 */ 11 12 public class HashMapDemo { 13 private static void testHashMapAPIs() { 14 /

【LeetCode从零单排】No.169 Majority Element(hashmap用法)

题目 Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume that the array is non-empty and the majority element always exist in the array. Credits:Special thanks to

java HashMap用法

它存储的内容是键值对(key-value)映射 void clear() Object clone() boolean containsKey(Object key) boolean containsValue(Object value) Set<Entry<K, V>> entrySet() V get(Object key) boolean isEmpty() Set<K> keySet() V put(K key, V value) void putAll(Map

并发编程(四):ThreadLocal从源码分析总结到内存泄漏

一.目录 1.ThreadLocal是什么?有什么用? 2.ThreadLocal源码简要总结? 3.ThreadLocal为什么会导致内存泄漏? 二.ThreadLocal是什么?有什么用? 引入话题:在并发条件下,如何正确获得共享数据?举例:假设有多个用户需要获取用户信息,一个线程对应一个用户.在mybatis中,session用于操作数据库,那么设置.获取操作分别是session.set().session.get(),如何保证每个线程都能正确操作达到想要的结果? /** * 回顾sync

Java中关于WeakReference和WeakHashMap的理解

新美大的10月11日的笔试中有一道选择题,让选择函数返回结果,代码如下: 1 private static String test(){ 2 String a = new String("a"); 3 WeakReference<String> b = new WeakReference<String>(a); 4 WeakHashMap<String, Integer> weakMap = new WeakHashMap<String, In

Java 容器

在实际问题中我们经常需要处理数据,单纯依靠数组来存储数据对开发来说非常困难,java提供了一套容器来方便我们编程.对java容器有一个整体的了解对我们来说非常重要,这样在需要特定容器时,不会手忙脚乱,本文主要介绍java的一些基本容器,而不要仅知道使用ArrayList.下面的图片是java中的简单容器分类   --图片来自网络 java定义了四种容器类型,List.Set.Queue和Map. 其中List.Set.Queue都实现了Collection接口,下面来看看这4中类型的容器. Co

第七章 集合

1 /***************** 2 ***第七章 集合 3 *******知识点: 4 **************1.Collection和Iterator接口 5 ******************1.1 Collection接口简介 6 ******************1.2 Iterator接口简介 7 ******************1.3 Map接口简介 8 **************2.Set接口 9 ******************2.1 HashSet

9.4-全栈Java笔记:Map接口和实现类

现实生活中,我们经常需要成对存储某些信息.比如,我们使用的微信,一个手机号只能对应一个微信账户.这就是一种成对存储的关系. Map就是用来存储"键(key)-值(value) 对".  Map类中存储的"键值对"通过键来标识,所以"键对象"不能重复. Map 接口的实现类有: HashMap.TreeMap.HashTable.Properties等. 常用的方法: 方法 说明 Object   put(Object key, Object va

数据加密与保存

//--------------------------------------------------------------------- // 1.加密算法: //--------------------------------------------------------------------- //加密或解密字符串,change加密.-change解密 public static String Encryption(String str, int change) { short s