replace


Description:

public bool replace (owned K key, owned V value)

Inserts a new key and value into a GenericSet similar to @set.

The difference is that if the key already exists in the GenericSet, it gets replaced by the new key. 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 old 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: Replace a value:

public static int main (string[] args) {
HashTable<string, string> table = new HashTable<string, string> (str_hash, str_equal);
table.insert ("1", "first string");
table.insert ("2", "second string");
table.insert ("3", "3. string");

// The old key gets replaced by the new key:
table.replace ("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.replace.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