# 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, 
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://r24zeng.gitbook.io/leetcode-notebook/useful-java-knowledge/hashmap.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
