RealmSet - Java SDK
On this page
New in version 10.6.0.
You can use the RealmSet data type
to manage a collection of unique keys. RealmSet
implements Java's
Set
interface, so it works just like the built-in HashSet
class,
except managed RealmSet
instances persist their contents to a
realm. RealmSet
instances that contain Realm objects
actually only store references to those objects, so deleting a
Realm object from a realm also deletes that object from
any RealmSet
instances that contain the object.
Because RealmSet
implements RealmCollection
, it has some useful
mathematical methods, such as sum
, min
, and max
. For a complete
list of available RealmSet
methods, see: the RealmSet API
reference.
Method Limitations
You cannot use the following Realm
methods on objects that contain
a field of type RealmSet
:
Realm.insert()
Realm.insertOrUpdate()
Realm.createAllFromJson()
Realm.createObjectFromJson()
Realm.createOrUpdateAllFromJson()
Realm.createOrUpdateObjectFromJson()
Usage
To create a field of type RealmSet
, define an object property of
type RealmSet<E>
, where E
defines the keys you would like to
store in your RealmSet
.
Add an object to a
RealmSet
with RealmSet.add()Add multiple objects with RealmSet.addAll()
Check if the set contains a specific object with RealmSet.contains()
Check if the set contains all of multiple objects with RealmSet.containsAll()
Notifications
To subscribe to changes to a RealmSet
, pass a
SetChangeListener
implementation to the RealmSet.addChangeListener method.
Your SetChangeListener
implementation must define an
onChange()
method, which accepts a reference to the changed RealmSet
and a set of changes as parameters. You can access the number of items
added to the set as well as the number of items removed from the set
through the SetChangeSet
parameter.