Maps (Key/Value)

Maps, or key-value pairs, are data structures in programming that store collections of unique keys associated with corresponding values. They offer efficient lookup, insertion, and deletion operations based on keys, providing fast access to values based on their associated keys. Maps are highly versatile and can be used to represent various relationships and associations between data elements. They are particularly useful for tasks such as indexing, caching, and storing configuration settings, where quick retrieval of values based on keys is essential.

Select Languages

Examples

C

// no built in implementation
// must be built from scratch
 

C#

using System.Collections.Generic;

Dictionary<string, string> carMap =
  new Dictionary<string, string>();

carMap.Add("Joe", "Toyota");
carMap.Add("Bob", "Mazda");
carMap.Add("Tom", "Ford");

try {
  Console.WriteLine(carMap["Tom"]);
  
  carMap.Remove("Tom");
  Console.WriteLine(carMap["Tom"]);
} catch{
  Console.WriteLine("Key not found!");
}

C++

#include <iostream>
#include <map>
using namespace std;

map<string, string> carMap;
carMap["Joe"] = "Toyota";
carMap["Bob"] = "Mazda";
carMap["Tom"] = "Ford";
cout << carMap["Tom"] << endl;
  
carMap.erase("Tom");
cout << carMap.size() << endl;

Go

import "fmt"

carMap := map[string]string{
  "Joe": "Toyota",
  "Bob": "Mazda",
  "Tom": "Ford" }
    
result := carMap["Tom"]
fmt.Println(result)
  
delete(carMap, "Tom")
_, found := carMap["Tom"]
fmt.Println(found)

Java

import java.util.*;

Map<String, String> carMap = new HashMap<>();
carMap.put("Joe", "Toyota");
carMap.put("Bob", "Mazda");
carMap.put("Tom", "Ford");

System.out.println(carMap.get("Tom") + "\n");

carMap.remove("Tom");
System.out.println(carMap.containsKey("Tom"));

JavaScript

const carMap = new Map();

carMap.set("Joe", "Toyota");
carMap.set("Bob", "Mazda");
carMap.set("Tom", "Ford");

console.log(carMap.get("Tom"));

carMap.delete('Tom');
console.log(carMap.keys());

Kotlin

 var carMap : HashMap<String, String>
   = HashMap<String, String> ();

carMap.put("Joe", "Toyota");
carMap.put("Bob", "Mazda");
carMap.put("Tom", "Ford");
println(carMap["Tom"])

carMap.remove("Tom");
println(carMap.containsKey("Tom"));

MatLab

carMap = dictionary();
carMap("Joe") = "Toyota";
carMap("Bob") = "Mazda";
carMap("Tom") = "Ford";

disp(carMap("Tom"))

carMap = remove(carMap, "Tom");
disp(isKey(carMap, "Tom"))

PHP

$ordinals = [
    'Joe' => 'Toyota',
    'Bob' => 'Mazda',
    'Tom' => 'Ford',
];

echo $ordinals['Tom'] . "\n";

unset($ordinals['Tom']);
echo count($ordinals);

Python

carMap = {}
carMap["Joe"] = "Toyota"
carMap["Bob"] = "Mazda"
carMap["Tom"] = "Ford"

print(carMap.get("Tom"))

carMap.pop("Tom")
print(carMap.keys())

R

rm(list = ls())
# create the list
carMap <- list()
 
# Build up key value pairs
carMap[[ "Joe" ]] <- "Toyota"
carMap[[ "Bob" ]] <- "Mazda"
carMap[[ "Tom" ]] <- "Ford"
 
print(carMap$"Tom")

carMap[["Tom"]] <- NULL
print(carMap$"Tom" != NULL)

Ruby

carMap = Hash.new
carMap["Joe"] = "Toyota"
carMap["Bob"] = "Mazda"
carMap["Tom"] = "Ford"

puts carMap["Tom"]

carMap.delete("Tom")
puts carMap.include?("Tom")

Rust

use std::collections::HashMap;

let mut carMap = HashMap::new();

carMap.insert(
  "Joe".to_string(), 
  "Toyota".to_string()
);

carMap.insert(
  "Bob".to_string(), 
  "Mazda".to_string()
);

carMap.insert(
  "Tom".to_string(), 
  "Ford".to_string()
);

println!("{:?}", carMap.get("Tom"));
carMap.remove("Tom");
println!(
  "{:?}", 
  carMap.contains_key("Tom")
);

Scala

import scala.collection.mutable.Map;

var carMap:Map[String, String] = Map();
 
carMap += ("Joe" -> "Toyota");
carMap += ("Bob" -> "Mazda");
carMap += ("Tom" -> "Ford");

println(carMap("Tom"))

carMap -= ("Tom");
println(carMap.contains("Tom"));

Swift

var carMap = [String: String]()

carMap["Joe"] = "Toyota"
carMap["Bob"] = "Mazda"
carMap["Tom"] = "Ford"

var selected:String =
  carMap["Tom"] ?? ""

print(selected);

carMap.removeValue(forKey: "Tom")
let keyCheck:Bool = carMap["Tom"] != nil
print(keyCheck)

TypeScript

const carMap:Map<string, string> =
  new Map();

carMap.set("Joe", "Toyota");
carMap.set("Bob", "Mazda");
carMap.set("Tom", "Ford");

console.log(carMap.get("Tom"));

carMap.delete('Tom');
console.log(carMap.keys());

Copyright 2025. All Rights Reserved. IronCodeMan.