RealmSet (beta) - Android SDK

New in version 10.6.0.


This feature is currently in beta. While we encourage you to try out the feature and give feedback, please be aware the API may change.

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.

RealmSet Limitations

Objects containing RealmSet fields currently cannot use the following Realm methods:

  • Realm.insert()
  • Realm.insertOrUpdate()
  • Realm.createAllFromJson()
  • Realm.createObjectFromJson()
  • Realm.createOrUpdateAllFromJson()
  • Realm.createOrUpdateObjectFromJson()

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.

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.

Give Feedback

On this page

  • Usage
  • Notifications