RealmDictionary - Java SDK
On this page
New in version 10.6.0.
You can use the RealmDictionary data type to manage a collection of
unique String
keys paired with values. RealmDictionary
implements Java's Map
interface, so it works just like the built-in
HashMap
class, except managed RealmDictionary
instances persist
their contents to a realm. RealmDictionary
instances that
contain Realm objects store references to those objects.
When you delete a Realm object from a realm, any
references to that object in a RealmDictionary
become null
values.
Usage
To create a field of type RealmDictionary
, define an object property
of type RealmDictionary<T>
, where T
defines the values you would
like to store in your RealmDictionary
. Currently, RealmDictionary
instances can only use keys of type String
.
- Add an object to a
RealmDictionary
with RealmDictionary.put() (or the[]
operator in Kotlin) - Add multiple objects to a
RealmDictionary
with RealmDictionary.putAll() - Check if the dictionary contains an specific key with RealmDictionary.containsKey()
- Check if the dictionary contains a specific value with RealmDictionary.containsValue()
Notifications
To subscribe to changes to a RealmDictionary
, pass a
MapChangeListener
implementation to the RealmSet.addChangeListener method.
Your MapChangeListener
implementation must define an
onChange()
method, which accepts a reference to the changed RealmDictionary
and a set of changes as parameters. You can access the keys
added to the dictionary as well as the keys removed from the dictionary
through the MapChangeSet
parameter.