Junior — Middle
Can you give an example of when and why data hashing is used?
sobes.tech AI
Answer from AI
Data hashing is used for fast searching, comparison, and integrity verification of data.
Examples of use:
- In collections such as
HashMaporHashSet, for quick access to elements by key. - To verify the integrity of files or messages (e.g., checksums).
- In authentication and password storage (password hashing).
Example in Java using HashMap:
import java.util.HashMap;
public class Example {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
map.put("apple", 3);
map.put("banana", 5);
System.out.println(map.get("apple")); // Fast access to value by key
}
}
Here, the hash function is used inside HashMap for quick determination of the element's position.