Set in Hack

The Hack Language supports various collection types including Set.
Sets in Hack Language simply represents an unordered associative collection with an ability of storing unique values.
Sets does not supports the concept of keys and thus it is not possible in case of sets to iterate through the keys.
Set is in initial state in the Hack language and does not support many of the inbuilt data types any massively reduces its use in the practical problems. Currently it  has support for int and string values.
In future their are possibilities that the features supported by set in Hack will be improved. There are several example of using this feature in Hack language. A simple example of using set in Hack language is shown below:

Using set in Hack language Facebook:

<?hh
 function main() {
  // Create a Set 
  $set = Set {"Val1", "Val2"};
  // Add elements 
  $set[] = "Val3";
  // Add elements using the add() method
  $set->add("Val4")->add("Val5");
  // Remove element by value
  $set->remove("Val1");
 // Testing if a value is present
  echo ($set->contains("Val1") ? "true" : "false") . "\n\n";
  foreach ($set as $item) {
    echo $item . "\n";
  }
}
main();
Output:
false 
Val4 
Val2 
Val5 
Val3 
In the above example, we have shown a simple program that uses sets. I have demonstrated the simple of creating a set that is by using the 'set' keyword. As shown in the program we can provide values for the set at the time of creation or we can add values to a set in one of the other two ways shown in the example.
The first way simply adds a value at the end. While the second approach uses the 'add' method to insert multiple values at a time in the set.
We can also remove the elements from the set by using the 'remove' method.
We may also check that whether a specific is present in the current set or not by using the 'contains' method .
Hope the above tutorial is helpful for you.For any queries related to the above topic please mention in the comments below.

0 comments:

Post a Comment

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