DEV Community

loading...

Rust Collections: DSL Operations on Maps

ssivakumar profile image Sivakumar Updated on ・3 min read

In this post, we'll explore different DSL operations that can be carried on Maps collections (HashMap & BTreeMap). In case, if you've not read the 1st part of this on Sequences, please check out here

HashMap

HashMap is a key-value pair collections of data structure. For more details, please check this page from rust documentation.

Some of the useful DSL operations on HashMap can be found below.

    // Create new HashMap
    let mut hm1: HashMap<_, _> = HashMap::new();
    hm1.insert("Siva".to_string(), 4);
    hm1.insert("Kumar".to_string(), 5);
    println!("{:?}", hm1);

    output: {"Siva": 4, "Kumar": 5}

    // HashMap can also be created from an Array like below
    let hm2: HashMap<&str, i32> = [("Siva", 4), ("Kumar", 5), ("Sivakumar", 4)].iter().cloned().collect();
    println!("{:?}", hm2);

    output: {"Siva": 4, "Kumar": 5, "Sivakumar": 4}

    // Check the total elements in HashMap
    println!("Length: {}", hm2.len());

    output: Length: 3

    // Perform `sum` actions on the values of HashMap
    // This will add each elements' value of HashMap
    println!("Sum of all values in HashMap: {}", hm2.values().sum::<i32>());

    output: Sum of all values in HashMap: 13

    // Perform `product` actions on the values of HashMap
    // This will multiply each elements' value of HashMap
    println!("Product of all values in HashMap: {}", hm2.values().product::<i32>());

    output: Product of all values in HashMap: 80

    // Another way of performing aggregte operation
    // Perform `fold` actions on the values of HashMap
    // This will multiply each elements of HashMap
    println!("Product of all values in HashMap: {}", hm2.values().fold(1, |x, y| x*y));

    output: Product of all values in HashMap: 80

    // Create Vector object from HashMap
    // In this example, we use values of HashMap to create new Vector
    let v1: Vec<_> = hm2.values().collect();
    println!("{:?}", v1);

    output: [4, 5, 4]

    // Create HashSet object from HashMap
    // In this example, we use values of HashMap to create new HashSet
    let s1: HashSet<_> = hm2.values().collect();
    println!("{:?}", s1);

    output: {4, 5}

    // Apply some filter operations on HashMap
    // Following example filter elements' Key stars_with Siva and 
    // return only that element's value
    let v2: Vec<_> = hm2.into_iter().filter(|e| e.0.starts_with("Siva")).map(|e| e.1).collect();
    println!("{:?}", v2);

    output: [4, 4]
Enter fullscreen mode Exit fullscreen mode
BTreeMap

A map based on BinaryTree. For more details, please check this page from rust documentation.

Some of the useful DSL operations on HashMap can be found below.

    // Create new BTreeMap
    let mut bm1: BTreeMap<_, _> = BTreeMap::new();
    bm1.insert("Siva".to_string(), 4);
    bm1.insert("Kumar".to_string(), 5);
    println!("{:?}", bm1);

    // BTreeMap can also be created from an Array like below
    let bm2: BTreeMap<&str, i32> = [("Siva", 4), ("Kumar", 5), ("Sivakumar", 4)].iter().cloned().collect();
    println!("{:?}", bm2);

    // Check the total elements in BTreeMap
    println!("Length: {}", bm2.len());

    // Perform `sum` actions on the values of BTreeMap
    // This will add each elements' value of BTreeMap
    println!("Sum of all values in BTreeMap: {}", bm2.values().sum::<i32>());

    // Perform `product` actions on the values of BTreeMap
    // This will multiply each elements' value of BTreeMap
    println!("Product of all values in BTreeMap: {}", bm2.values().product::<i32>());

    // Another way of performing aggregte operation
    // Perform `fold` actions on the values of BTreeMap
    // This will multiply each elements of BTreeMap
    println!("Product of all values in BTreeMap: {}", bm2.values().fold(1, |x, y| x*y));

    // Create Vector object from BTreeMap
    // In this example, we use values of BTreeMap to create new Vector
    let v1: Vec<_> = bm2.values().collect();
    println!("{:?}", v1);

    // Create HashSet object from BTreeMap
    // In this example, we use values of BTreeMap to create new HashSet
    let s1: HashSet<_> = bm2.values().collect();
    println!("{:?}", s1);

    // Apply some filter operations on BTreeMap
    // Following example filter elements' Key equals Siva and 
    // return only that element's value
    let v2: Vec<_> = bm2.into_iter().filter(|e| e.0 == "Siva").map(|e| e.1).collect();
    println!("{:?}", v2);
Enter fullscreen mode Exit fullscreen mode

Please kindly share your feedback.

Happy reading!!!

Discussion (0)

Forem Open with the Forem app