Logo

Programming-Idioms

History of Idiom 13 > diff from v26 to v27

Edit summary for version 27 by :

Version 26

2015-09-13, 12:30:10

Version 27

2015-10-29, 14:05:12

Idiom #13 Iterate over map keys and values

Print each key k with its value x from an associative array mymap

Idiom #13 Iterate over map keys and values

Print each key k with its value x from an associative array mymap

Code
Enum.each(mymap, fn({key, value}) ->
  IO.puts("#{key} => #{value}")
end)
Code
Enum.each(mymap, fn({key, value}) ->
  IO.puts("#{key} => #{value}")
end)
Doc URL
http://elixir-lang.org/docs/v1.0/elixir/Enum.html#each/2
Doc URL
http://elixir-lang.org/docs/v1.0/elixir/Enum.html#each/2
Demo URL
http://play.elixirbyexample.com/s/ed1f67705b
Demo URL
http://play.elixirbyexample.com/s/ed1f67705b
Imports
uses fgl;
type TMyMap = specialize TFPGMap<String, Integer>;
Imports
uses fgl;
type TMyMap = specialize TFPGMap<String, Integer>;
Code
for i := 0 to mymap.Count - 1 do
  WriteLn(mymap.Keys[i], '=', mymap.Data[i]);
Code
for i := 0 to mymap.Count - 1 do
  WriteLn(mymap.Keys[i], '=', mymap.Data[i]);
Doc URL
http://wiki.freepascal.org/Generics#fgl_unit
Doc URL
http://wiki.freepascal.org/Generics#fgl_unit
Imports
import std.stdio;
Imports
import std.stdio;
Code
int[string] mymap = ["Hello":1 , "World":2];
foreach (k, v; mymap)
    writeln("Key: ", k, " Value: ", v);
Code
int[string] mymap = ["Hello":1 , "World":2];
foreach (k, v; mymap)
    writeln("Key: ", k, " Value: ", v);
Comments bubble
D has built-in associative array
Comments bubble
D has built-in associative array
Imports
use std::collections::BTreeMap;
Imports
use std::collections::BTreeMap;
Code
for (key, val) in &mymap {
    println!("Key={key}, Value={val}", key=key, val=val);
}
Code
for (key, val) in &mymap {
    println!("Key={key}, Value={val}", key=key, val=val);
}
Comments bubble
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`.
Comments bubble
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`.
Demo URL
http://is.gd/FnGX0Z
Demo URL
http://is.gd/FnGX0Z
Code
for (k in mymap) {
   if (mymap.hasOwnProperty(k))
      console.log("key="+k+", value="+mymap[k]);
}
Code
for (k in mymap) {
   if (mymap.hasOwnProperty(k))
      console.log("key="+k+", value="+mymap[k]);
}
Comments bubble
The if clause using hasOwnProperty filters out any properties that mymap may have inherited from its prototype.
Comments bubble
The if clause using hasOwnProperty filters out any properties that mymap may have inherited from its prototype.
Code
mymap.each {|k, x| puts "Key= #{k}  Value=#{x}"}
Code
mymap.each {|k, x| puts "Key= #{k}  Value=#{x}"}
Imports
import "fmt"
Imports
import "fmt"
Code
for k, x := range mymap {
  fmt.Println("Key =", k, ", Value =", x)
}
Code
for k, x := range mymap {
  fmt.Println("Key =", k, ", Value =", x)
}
Comments bubble
Do not rely on the order of the traversal ! The order is undefined and is intentionaly randomized by the Go runtime.
Comments bubble
Do not rely on the order of the traversal ! The order is undefined and is intentionaly randomized by the Go runtime.
Demo URL
http://play.golang.org/p/5BMpuLiuXj
Demo URL
http://play.golang.org/p/5BMpuLiuXj
Code
for (Map.Entry<Object, Object> entry : mymap.entrySet()) {
    Object k = entry.getKey();
    Object x = entry.getValue();
    System.out.println("Key=" + k + ", Value=" + x);
}
Code
for (Map.Entry<Object, Object> entry : mymap.entrySet()) {
    Object k = entry.getKey();
    Object x = entry.getValue();
    System.out.println("Key=" + k + ", Value=" + x);
}
Comments bubble
Instead of Object, prefer using sensible key type and value type for your map.
Comments bubble
Instead of Object, prefer using sensible key type and value type for your map.
Origin
http://stackoverflow.com/questions/1066589/java-iterate-through-hashmap#answer-1066607
Origin
http://stackoverflow.com/questions/1066589/java-iterate-through-hashmap#answer-1066607