@set


Description:

[ CCode ( cname = "g_hash_table_insert" ) ]
public bool @set (owned K key, owned V value)

Inserts a new key and value into a GenericSet.

If the key already exists in the GenericSet its current value is replaced with the new value. If you supplied a value_destroy_func when creating the GenericSet , the old value is freed using that function. If you supplied a key_destroy_func when creating the GenericSet, the passed key is freed using that function.

Starting from GLib 2.40, this function returns a boolean value to indicate whether the newly added value was already in the hash table or not.

Example: Associate a value with a key:

public static int main (string[] args) {
HashTable<string, string> table = new HashTable<string, string> (str_hash, str_equal);
string key1 = "1"; string val1 = "first string";
string key2 = "2"; string val2 = "second string";
string key3 = "3"; string val3 = "3. value";

// Use (owned) to avoid unnecessary copies:
table.set ((owned) key1, (owned) val1);
table.set ((owned) key2, (owned) val2);
table.set ((owned) key3, (owned) val3);

// Fields are owned by the table:
assert (key1 == null && key2 == null && key3 == null);
assert (val1 == null && val2 == null && val3 == null);

// Overwrite existing entry:
table.set ("3", "third string");


// Output:
// ``1 => first string``
// ``2 => second string``
// ``3 => third string``
table.foreach ((key, val) => {
print ("%s => %s\n", key, val);
});

return 0;
}

valac --pkg glib-2.0 GLib.HashTable.set.vala

Parameters:

key

a key to insert

value

the value to associate with the key

hash_table

a GenericSet

Returns:

true if the key did not exist yet