Map in Hack Language Facebook

Hack Language supports Map that are simply an ordered dictionary-style collection. Map in Hack Language Facebook is used in quite similar fashion as in other languages.
In case of Map, elements are generally stored as key/value pairs means simply a key representing a value.
The keys that are generally of type string or int. The other types are not supported by the Map in Hack language yet.
Hack allows Maps to maintain the order in which the elements are inserted in the map, that means whenever we iterate through a Map, the elements are visited in the same order as they are inserted.
The various operation like insertion, removal and search operations are performed in O(lg n) time or better

Example:

<?hh
function main() {
  $mymap = Map {"val1" => 1, "val2" => 2};
  $mymap["val3"] =3;
  echo $mymap["val1"] ."\n";
  echo $mymap->get("val2") . "\n\n";
  $mymap->remove("val3");
  echo ($mymap->contains("val1") ?"true" : "false") . "\n\n";
  foreach ($mymap as $val) {
    echo $val . "\n";
  }
  foreach ($map as $key => $val) {
    echo $key . ": " . $val . "\n";
  }
}
main();
Output:
1
2
true
1
2
val1 : 1
val2 : 2

As we can see in the above example, I have demonstrated that how we can simply create a Map by using the Map keyword.
We can assign values to a Map at the time of creating the Map or after creating the map as shown in the above example program.
We can iterate through the Map by simply using the for-each loop or by using an advanced version of for-each loop that allows the access to both the key and value pair of each element in the Map.
So, the Hack language provides its users the flexibility of using the Map feature according to their requirement.
Hope the above tutorial is useful for you. In case of any queries or appreciations please mention it in comments below.

0 comments:

Post a Comment

Note: only a member of this blog may post a comment.