Hash Maps and Hash Sets

Introduction

In this lesson, we'll explore hash maps and hash sets, their properties, and their usage in Rust.

Key Points

  • Hash Maps: A data structure that stores key-value pairs with efficient lookups, insertions, and deletions.
  • Hash Sets: A data structure that stores unique elements with efficient membership checking.

Hash Maps

A hash map is a collection of key-value pairs where each key is unique. It allows for efficient data retrieval based on keys.

use std::collections::HashMap;

fn main() {
    let mut map = HashMap::new();

    // Inserting key-value pairs
    map.insert("name", "Alice");
    map.insert("age", "30");
    map.insert("city", "New York");

    // Accessing values
    if let Some(name) = map.get("name") {
        println!("Name: {}", name);
    }

    // Iterating over key-value pairs
    for (key, value) in &map {
        println!("{}: {}", key, value);
    }

    // Removing a key-value pair
    map.remove("age");

    // Checking if a key exists
    if map.contains_key("city") {
        println!("City key exists");
    }
}

Code Explanation

  • insert adds a key-value pair to the map.
  • get retrieves a value associated with a key.
  • remove deletes a key-value pair from the map.
  • contains_key checks if a key exists in the map.

Hash Sets

A hash set is a collection of unique elements with no duplicates. It allows for efficient membership testing and insertion.

use std::collections::HashSet;

fn main() {
    let mut set = HashSet::new();

    // Inserting elements
    set.insert("apple");
    set.insert("banana");
    set.insert("cherry");

    // Checking if an element exists
    if set.contains("banana") {
        println!("Banana is in the set");
    }

    // Removing an element
    set.remove("apple");

    // Iterating over elements
    for item in &set {
        println!("{}", item);
    }
}

Code Explanation

  • insert adds an element to the set.
  • contains checks if an element is in the set.
  • remove deletes an element from the set.

Real World Examples

Hash Maps

Hash maps are widely used in situations where you need to quickly look up values based on a unique key. For instance, in a contact management system, you can use a hash map to store and retrieve contact information using phone numbers as keys.

How It Works:

  • Insert: Add a contact with a unique phone number.
  • Get: Retrieve a contact's information using their phone number.
  • Remove: Delete a contact when it's no longer needed.

Hash Sets

Hash sets are useful for managing collections of unique items. For example, in a voting system, you can use a hash set to keep track of users who have already voted, ensuring that each user only votes once.

How It Works:

  • Insert: Add a user ID to the set when they vote.
  • Contains: Check if a user ID is already in the set to prevent duplicate votes.
  • Remove: If needed, remove a user ID from the set (e.g., if a vote is retracted).

Conclusion

Hash maps and hash sets are powerful data structures for efficient data management and retrieval. By implementing these structures in Rust, you can handle a wide range of applications and improve the performance of your programs.