Class HashTable<K,V>

java.lang.Object
java.util.AbstractMap<@Nullable K, @Nullable V>
org.gnome.glib.HashTable<K,V>
All Implemented Interfaces:
Map<K,V>, Proxy

@NullMarked public class HashTable<K,V> extends AbstractMap<@Nullable K, @Nullable V> implements Proxy
The GHashTable struct is an opaque data structure to represent a hash table. The keys and values of the Java class can be pointers (MemorySegment objects), strings, primitive values or native objects (implementing the Proxy interface).

This class is intended to help Java developers deal with native functions that require or return a GHashTable. It is not meant to be used as a replacement for Java's own HashMap.

  • Constructor Details

    • HashTable

      public HashTable(MemorySegment address, @Nullable Function<MemorySegment, K> makeKey, @Nullable Function<MemorySegment, V> makeValue)
      Create a HashTable proxy instance for the provided memory address.
      Parameters:
      address - the memory address of the native object
      makeKey - a function that creates a K from a pointer
      makeValue - a function that creates a V from a pointer
    • HashTable

      public HashTable(MemorySegment address)
      Create a HashTable proxy instance for the provided memory address. The hashtable contains MemorySegment keys and values.
      Parameters:
      address - the memory address of the native object
    • HashTable

      public HashTable(@Nullable HashFunc hashFunc, @Nullable EqualFunc keyEqualFunc, Function<MemorySegment, K> makeKey, Function<MemorySegment, V> makeValue)
      Construct a new HashTable using g_hash_table_new.
      Parameters:
      hashFunc - a function to create a hash value from a key
      keyEqualFunc - a function to check two keys for equality
      makeKey - a function that creates a K from a pointer
      makeValue - a function that creates a V from a pointer
  • Method Details

    • handle

      public MemorySegment handle()
      Description copied from interface: Proxy
      Get the native memory address of the object.
      Specified by:
      handle in interface Proxy
      Returns:
      the native memory address
    • entrySet

      public Set<Map.Entry<K,V>> entrySet()
      Specified by:
      entrySet in interface Map<K,V>
      Specified by:
      entrySet in class AbstractMap<@Nullable K, @Nullable V>
    • put

      public V put(@Nullable K key, @Nullable V value)
      Specified by:
      put in interface Map<K,V>
      Overrides:
      put in class AbstractMap<@Nullable K, @Nullable V>
    • getType

      public static @Nullable Type getType()
      Get the GType of the HashTable class
      Returns:
      the GType
    • new_

      public static HashTable<MemorySegment, MemorySegment> new_(@Nullable HashFunc hashFunc, @Nullable EqualFunc keyEqualFunc)
      Creates a new GHashTable with a reference count of 1.

      Hash values returned by hashFunc are used to determine where keys are stored within the GHashTable data structure. The g_direct_hash(), g_int_hash(), g_int64_hash(), g_double_hash() and g_str_hash() functions are provided for some common types of keys. If hashFunc is null, g_direct_hash() is used.

      keyEqualFunc is used when looking up keys in the GHashTable. The g_direct_equal(), g_int_equal(), g_int64_equal(), g_double_equal() and g_str_equal() functions are provided for the most common types of keys. If keyEqualFunc is null, keys are compared directly in a similar fashion to g_direct_equal(), but without the overhead of a function call. keyEqualFunc is called with the key from the hash table as its first parameter, and the user-provided key to check against as its second.

      Parameters:
      hashFunc - a function to create a hash value from a key
      keyEqualFunc - a function to check two keys for equality
      Returns:
      a new GHashTable
    • add

      public boolean add(@Nullable K key)
      This is a convenience function for using a GHashTable as a set. It is equivalent to calling g_hash_table_replace() with key as both the key and the value.

      In particular, this means that if key already exists in the hash table, then the old copy of key in the hash table is freed and key replaces it in the table.

      When a hash table only ever contains keys that have themselves as the corresponding value it is able to be stored more efficiently. See the discussion in the section description.

      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.

      Parameters:
      key - a key to insert
      Returns:
      true if the key did not exist yet
    • contains

      public boolean contains(@Nullable K key)
      Checks if key is in this GLib.HashTable.
      Parameters:
      key - a key to check
      Returns:
      true if key is in this GLib.HashTable, false otherwise.
    • destroy

      public void destroy()
      Destroys all keys and values in the GHashTable and decrements its reference count by 1. If keys and/or values are dynamically allocated, you should either free them first or create the GHashTable with destroy notifiers using g_hash_table_new_full(). In the latter case the destroy functions you supplied will be called on all keys and values during the destruction phase.
    • find

      public V find(HRFunc predicate)
      Calls the given function for key/value pairs in the GHashTable until predicate returns true. The function is passed the key and value of each pair, and the given userData parameter. The hash table may not be modified while iterating over it (you can't add/remove items).

      Note, that hash tables are really only optimized for forward lookups, i.e. g_hash_table_lookup(). So code that frequently issues g_hash_table_find() or g_hash_table_foreach() (e.g. in the order of once per every entry in a hash table) should probably be reworked to use additional or different data structures for reverse lookups (keep in mind that an O(n) find/foreach operation issued for all n values in a hash table ends up needing O(n*n) operations).

      Parameters:
      predicate - function to test the key/value pairs for a certain property
      Returns:
      The value of the first key/value pair is returned, for which predicate evaluates to true. If no pair with the requested property is found, null is returned.
    • foreach

      public void foreach(HFunc func)
      Calls the given function for each of the key/value pairs in the GHashTable. The function is passed the key and value of each pair, and the given userData parameter. The hash table may not be modified while iterating over it (you can't add/remove items). To remove all items matching a predicate, use g_hash_table_foreach_remove().

      The order in which g_hash_table_foreach() iterates over the keys/values in the hash table is not defined.

      See g_hash_table_find() for performance caveats for linear order searches in contrast to g_hash_table_lookup().

      Parameters:
      func - the function to call for each key/value pair
    • foreachRemove

      public int foreachRemove(HRFunc func)
      Calls the given function for each key/value pair in the GHashTable. If the function returns true, then the key/value pair is removed from the GHashTable. If you supplied key or value destroy functions when creating the GHashTable, they are used to free the memory allocated for the removed keys and values.

      See GHashTableIter for an alternative way to loop over the key/value pairs in the hash table.

      Parameters:
      func - the function to call for each key/value pair
      Returns:
      the number of key/value pairs removed
    • foreachSteal

      public int foreachSteal(HRFunc func)
      Calls the given function for each key/value pair in the GHashTable. If the function returns true, then the key/value pair is removed from the GHashTable, but no key or value destroy functions are called.

      See GHashTableIter for an alternative way to loop over the key/value pairs in the hash table.

      Parameters:
      func - the function to call for each key/value pair
      Returns:
      the number of key/value pairs removed.
    • getKeys

      public List<K> getKeys()
      Retrieves every key inside this GLib.HashTable. The returned data is valid until changes to the hash release those keys.

      This iterates over every entry in the hash table to build its return value. To iterate over the entries in a GHashTable more efficiently, use a GHashTableIter.

      Returns:
      a GList containing all the keys inside the hash table. The content of the list is owned by the hash table and should not be modified or freed. Use g_list_free() when done using the list.
    • getKeysAsArray

      public MemorySegment[] getKeysAsArray()
      Retrieves every key inside this GLib.HashTable, as an array.

      The returned array is null-terminated but may contain null as a key. Use length to determine the true length if it's possible that null was used as the value for a key.

      Note: in the common case of a string-keyed GHashTable, the return value of this function can be conveniently cast to (const gchar **).

      This iterates over every entry in the hash table to build its return value. To iterate over the entries in a GHashTable more efficiently, use a GHashTableIter.

      You should always free the return result with g_free(). In the above-mentioned case of a string-keyed hash table, it may be appropriate to use g_strfreev() if you call g_hash_table_steal_all() first to transfer ownership of the keys.

      Returns:
      a null-terminated array containing each key from the table.
    • getKeysAsPtrArray

      public MemorySegment[] getKeysAsPtrArray()
      Retrieves every key inside this GLib.HashTable, as a GPtrArray. The returned data is valid until changes to the hash release those keys.

      This iterates over every entry in the hash table to build its return value. To iterate over the entries in a GHashTable more efficiently, use a GHashTableIter.

      You should always unref the returned array with g_ptr_array_unref().

      Returns:
      a GPtrArray containing each key from the table. Unref with with g_ptr_array_unref() when done.
    • getValues

      public List<V> getValues()
      Retrieves every value inside this GLib.HashTable. The returned data is valid until this GLib.HashTable is modified.

      This iterates over every entry in the hash table to build its return value. To iterate over the entries in a GHashTable more efficiently, use a GHashTableIter.

      Returns:
      a GList containing all the values inside the hash table. The content of the list is owned by the hash table and should not be modified or freed. Use g_list_free() when done using the list.
    • getValuesAsPtrArray

      public MemorySegment[] getValuesAsPtrArray()
      Retrieves every value inside this GLib.HashTable, as a GPtrArray. The returned data is valid until changes to the hash release those values.

      This iterates over every entry in the hash table to build its return value. To iterate over the entries in a GHashTable more efficiently, use a GHashTableIter.

      You should always unref the returned array with g_ptr_array_unref().

      Returns:
      a GPtrArray containing each value from the table. Unref with with g_ptr_array_unref() when done.
    • insert

      public boolean insert(@Nullable K key, @Nullable V value)
      Inserts a new key and value into a GHashTable.

      If the key already exists in the GHashTable its current value is replaced with the new value. If you supplied a valueDestroyFunc when creating the GHashTable, the old value is freed using that function. If you supplied a keyDestroyFunc when creating the GHashTable, 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.

      Parameters:
      key - a key to insert
      value - the value to associate with the key
      Returns:
      true if the key did not exist yet
    • lookup

      public @Nullable V lookup(@Nullable K key)
      Looks up a key in a GHashTable. Note that this function cannot distinguish between a key that is not present and one which is present and has the value null. If you need this distinction, use g_hash_table_lookup_extended().
      Parameters:
      key - the key to look up
      Returns:
      the associated value, or null if the key is not found
    • lookupExtended

      public boolean lookupExtended(@Nullable K lookupKey, @Nullable Out<K> origKey, @Nullable Out<V> value)
      Looks up a key in the GHashTable, returning the original key and the associated value and a gboolean which is true if the key was found. This is useful if you need to free the memory allocated for the original key, for example before calling g_hash_table_remove().

      You can actually pass null for lookupKey to test whether the null key exists, provided the hash and equal functions of this GLib.HashTable are null-safe.

      Parameters:
      lookupKey - the key to look up
      origKey - return location for the original key
      value - return location for the value associated with the key
      Returns:
      true if the key was found in the GHashTable
    • newSimilar

      public HashTable<K,V> newSimilar()
      Creates a new GHashTable like g_hash_table_new_full() with a reference count of 1.

      It inherits the hash function, the key equal function, the key destroy function, as well as the value destroy function, from this GLib.HashTable.

      The returned hash table will be empty; it will not contain the keys or values from this GLib.HashTable.

      Returns:
      a new GHashTable
    • ref

      public HashTable<K,V> ref()
      Atomically increments the reference count of this GLib.HashTable by one. This function is MT-safe and may be called from any thread.
      Returns:
      the passed in GHashTable
    • remove_

      public boolean remove_(@Nullable K key)
      Removes a key and its associated value from a GHashTable.

      If the GHashTable was created using g_hash_table_new_full(), the key and value are freed using the supplied destroy functions, otherwise you have to make sure that any dynamically allocated values are freed yourself.

      This method calls g_hash_table_remove(). It does not override or replace AbstractMap.remove(Object).

      Parameters:
      key - the key to remove
      Returns:
      true if the key was found and removed from the GHashTable
    • removeAll

      public void removeAll()
      Removes all keys and their associated values from a GHashTable.

      If the GHashTable was created using g_hash_table_new_full(), the keys and values are freed using the supplied destroy functions, otherwise you have to make sure that any dynamically allocated values are freed yourself.

    • replace_

      public boolean replace_(@Nullable K key, @Nullable V value)
      Inserts a new key and value into a GHashTable similar to g_hash_table_insert(). The difference is that if the key already exists in the GHashTable, it gets replaced by the new key. If you supplied a valueDestroyFunc when creating the GHashTable, the old value is freed using that function. If you supplied a keyDestroyFunc when creating the GHashTable, 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.

      This method calls g_hash_table_replace(). It does not override or replace Map.replace(Object, Object).

      Parameters:
      key - a key to insert
      value - the value to associate with the key
      Returns:
      true if the key did not exist yet
    • size

      public int size()
      Returns the number of elements contained in the GHashTable.
      Specified by:
      size in interface Map<K,V>
      Overrides:
      size in class AbstractMap<@Nullable K, @Nullable V>
      Returns:
      the number of key/value pairs in the GHashTable.
    • steal

      public boolean steal(@Nullable K key)
      Removes a key and its associated value from a GHashTable without calling the key and value destroy functions.
      Parameters:
      key - the key to remove
      Returns:
      true if the key was found and removed from the GHashTable
    • stealAll

      public void stealAll()
      Removes all keys and their associated values from a GHashTable without calling the key and value destroy functions.
    • stealExtended

      public boolean stealExtended(@Nullable K lookupKey, @Nullable Out<K> stolenKey, @Nullable Out<V> stolenValue)
      Looks up a key in the GHashTable, stealing the original key and the associated value and returning true if the key was found. If the key was not found, false is returned.

      If found, the stolen key and value are removed from the hash table without calling the key and value destroy functions, and ownership is transferred to the caller of this method, as with g_hash_table_steal(). That is the case regardless whether stolenKey or stolenValue output parameters are requested.

      You can pass null for lookupKey, provided the hash and equal functions of this GLib.HashTable are null-safe.

      The dictionary implementation optimizes for having all values identical to their keys, for example by using g_hash_table_add(). Before 2.82, when stealing both the key and the value from such a dictionary, the value was null. Since 2.82, the returned value and key will be the same.

      Parameters:
      lookupKey - the key to look up
      stolenKey - return location for the original key
      stolenValue - return location for the value associated with the key
      Returns:
      true if the key was found in the GHashTable
    • unref

      public void unref()
      Atomically decrements the reference count of this GLib.HashTable by one. If the reference count drops to 0, all keys and values will be destroyed, and all memory allocated by the hash table is released. This function is MT-safe and may be called from any thread.