Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!

Idiom #13 Iterate over map keys and values

Access each key k with its value x from an associative array mymap, and print them.

Traversing each key and each associated object of mymap
with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Strings.Hash;

use Ada.Containers;
for C in My_Map.Iterate loop
   Put_Line ("Key = " & Key (C) & ", Value = " & Element (C));
end loop;
(doseq [[k x] mymap]
  (println k ":" x))
#include <iostream>
for (const auto& [k, x]: mymap) {
	std::cout << "Key: " << k << " Value: " << x << '\n';
}
#include <iostream>
for (auto entry : mymap) {
  auto k = entry.first;
  auto x = entry.second;
  std::cout << k << ": " << x << "\n";
}
#include <iostream>
for (const auto& kx: mymap) {
	std::cout << "Key: " << kx.first << " Value: " << kx.second << std::endl;
}
foreach(var entry in map)
{
    Console.WriteLine("Key=" + entry.Key + ", Value=" + entry.Value);
}
import std.stdio;
int[string] mymap = ["Hello":1 , "World":2];
foreach (k, v; mymap)
    writeln("Key: ", k, " Value: ", v);
mymap.forEach((k, v) => print('Key=$k, Value=$v'));
Enum.each(mymap, fn({k, x}) ->
  IO.puts("#{k} => #{x}")
end)
for {k, x} <- mymap do
  IO.puts("#{k} => #{x}")
end
maps:fold(
	fun(K, V, ok) ->
		io:format("~p: ~p~n", [K, V])
	end, ok, MyMap).
import "fmt"
for k, x := range mymap {
  fmt.Println("Key =", k, ", Value =", x)
}
mymap.each { k, x ->
	println "Key $k - Value $x"
}
import Data.List (intercalate)
import qualified Data.Map as Map
let f k v = [show k, " = ", show v]
    mapped = Map.mapWithKeys f mymap
in putStrLn $ intercalate "," $ mapped
Object.entries(mymap).forEach(([key, value]) => {
	console.log('key:', key, 'value:', value);
});
for (const key in mymap) {
    console.log('key:', key, 'value:', mymap[key]);
}
for (Map.Entry<Object, Object> entry : mymap.entrySet()) {
    Object k = entry.getKey();
    Object x = entry.getValue();
    System.out.println("Key=" + k + ", Value=" + x);
}
import java.util.Map;
mymap.forEach((k,x) -> System.out.println("Key=" + k + ", Value=" + x));
import static java.lang.System.out;
import java.util.Map.Entry;
K k;
X x;
for (Entry<K, X> e : mymap.entrySet()) {
    k = e.getKey();
    x = e.getValue();
    out.println(e);
}
mymap.forEach { k, v -> println("$k -> $v") }
mymap.entries.forEach { print("${it.key} ${it.value}") }
(loop for k being each hash-key of mymap
      for x being each hash-value of mymap                                                                            
      do (format t "~A ~A~%" k x)))
for k, x in pairs(mymap) do
	print('Key: '..k..', Value: '..x)
end
@import Foundation;
for (NSString *k in mymap)
  NSLog(@"Key %@, value %@",k,mymap[k]);
foreach ($mymap as $k=>$x)
{
    echo "Key=$k, Value=$x <br>";
}
uses fgl;
type TMyMap = specialize TFPGMap<String, Integer>;
for i := 1 to mymap.Count 		do
  WriteLn(mymap.Keys[i], '=', mymap.Data[i]);
printf "Key=%s, Value=%s\n",$_,$mymap{$_} foreach (sort keys %mymap);

while (my ($k, $x) = each %mymap) {
    print "Key=$k, Value=$x\n";
}
forall(get_dict(K, D, V),
       format("~w:~w~n", [K, V]))
for k, v in mymap.items():
    print(k, v)
for x in mymap.items(): print(x)
mymap.each {|k, x| puts "Key= #{k}  Value=#{x}"}
use std::collections::BTreeMap;
for (k, x) in &mymap {
    println!("Key={key}, Value={val}", key=k, val=x);
}
val x = Map("a" -> 1, "b" -> 2, "c" -> 3)
x.foreach{ case (key, value) => println(s"$key => $value")}
mymap.foreach { case (k, x) => println(s"$k => $x") }
mymap keysAndValuesDo: [ :k :x |
	Transcript cr;
		show: 'Key = ', k printString;
		show: ', Value = ', x printString]

New implementation...