Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- Ada
- Clojure
- C++
- C++
- C++
- C#
- D
- Dart
- Elixir
- Elixir
- Erlang
- Go
- Groovy
- Haskell
- JS
- JS
- Java
- Java
- Java
- Kotlin
- Kotlin
- Lisp
- Lua
- Obj-C
- PHP
- Pascal
- Perl
- Perl
- Prolog
- Python
- Python
- Ruby
- Rust
- Scala
- Scala
- Smalltalk
(doseq [[k x] mymap]
(println k ":" x))
for (auto entry : mymap) {
auto k = entry.first;
auto x = entry.second;
std::cout << k << ": " << x << "\n";
}
for (const auto& kx: mymap) {
std::cout << "Key: " << kx.first << " Value: " << kx.second << std::endl;
}
std::endl flushes stdout.
foreach(var entry in map)
{
Console.WriteLine("Key=" + entry.Key + ", Value=" + entry.Value);
}
Enum.each(mymap, fn({k, x}) ->
IO.puts("#{k} => #{x}")
end)
for {k, x} <- mymap do
IO.puts("#{k} => #{x}")
end
for k, x := range mymap {
fmt.Println("Key =", k, ", Value =", x)
}
Do not rely on the order of the traversal ! The order is undefined and is intentionaly randomized by the Go runtime.
Object.entries(mymap).forEach(([key, value]) => {
console.log('key:', key, 'value:', value);
});
for (const key in mymap) {
console.log('key:', key, 'value:', mymap[key]);
}
K k;
X x;
for (Entry<K, X> e : mymap.entrySet()) {
k = e.getKey();
x = e.getValue();
out.println(e);
}
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
foreach ($mymap as $k=>$x)
{
echo "Key=$k, Value=$x <br>";
}
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);
one-line style with keys sorted
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}"}
for (k, x) in &mymap {
println!("Key={key}, Value={val}", key=k, val=x);
}
You can also print collections in a 'nice' way with `println!("{:?}", mymap);` which is the Debug-representation. You can also use "{:#?}" for the pretty version.
This example works the same if you replace BTreeMap with HashMap.
This example works the same if you replace BTreeMap with HashMap.
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]