Câu hỏi phỏng vấn Android
Câu hỏi

Explain how HashMap works

Câu trả lời

How HashMap Works in Android

A HashMap in Android is a data structure that stores key-value pairs. It is part of the Java Collections Framework and is implemented as a hash table. Here's how it works:

1. Hashing

When you add a key-value pair to a HashMap, the key is hashed using a hash function. The hash function converts the key into an integer, which is then used to determine the index where the key-value pair will be stored in the underlying array.

2. Collision Handling

Since multiple keys can hash to the same index, collisions can occur. HashMap uses separate chaining to handle collisions. Each index in the underlying array is associated with a linked list (or a tree, depending on the size of the list). When a collision occurs, the key-value pair is added to the linked list at the corresponding index.

3. Retrieving Values

To retrieve a value from the HashMap, the key is hashed using the same hash function. The resulting index is used to access the linked list at that index. The list is then traversed to find the key-value pair with the matching key.

4. Load Factor and Resizing

HashMap has a load fact...

middle

middle

Gợi ý câu hỏi phỏng vấn

middle

What is the difference between Activity and Context ?

middle

What is Android Data Binding?

expert

What happens if the user navigates away or closes the app while I still have a reference to the Activity
the user just closed in my AsyncTask ?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào