> For the complete documentation index, see [llms.txt](https://r24zeng.gitbook.io/leetcode-notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://r24zeng.gitbook.io/leetcode-notebook/useful-java-knowledge/hashmap.md).

# HashMap

### Initialization

```java
// must use object, can't use primitive type
// array based data structure
Map<Object, Object> map = new HashMap<>();
```

### Useful methods

```java
map.size();
map.isEmpty();   // return boolean
map.put(key, value);  // return value, O(1)
map.get(key);  // return value, O(1)
map.containsKey(key); // boolean, O(1)
map.remove(key); // return value, O(1)

map.keySet();   // return a set of key, Set<key>
for (String str: map.KeySet()) {} // 遍历key


map.entrySet();   // return a set of entry, 
```
