E
- type of RealmObject
stored in the collection.public interface RealmCollection<E> extends Collection<E>
RealmCollection
is the root of the collection hierarchy that Realm supports. It defines operations on data
collections and the behavior that they will have in all implementations of RealmCollection
s.
Realm collections are "live" views to the underlying data. This means that they automatically will be kept up to
date. As a consequence, using methods like Collections.unmodifiableCollection(Collection)
will not prevent
a collection from being modified.
Modifier and Type | Method and Description |
---|---|
double |
average(String fieldName)
Returns the average of a given field.
|
boolean |
contains(Object object)
Tests whether this
Collection contains the specified object. |
boolean |
deleteAllFromRealm()
This deletes all objects in the collection from the underlying Realm as well as from the collection.
|
RealmCollection<E> |
freeze()
Returns a frozen snapshot of this collection.
|
boolean |
isLoaded()
Checks if a collection has finished loading its data yet.
|
boolean |
isManaged()
Checks if the collection is managed by Realm.
|
boolean |
isValid()
Checks if the collection is still valid to use, i.e., the
Realm instance hasn't been closed. |
boolean |
load()
Blocks the collection until all data are available.
|
Number |
max(String fieldName)
Finds the maximum value of a field.
|
Date |
maxDate(String fieldName)
Finds the maximum date.
|
Number |
min(String fieldName)
Finds the minimum value of a field.
|
Date |
minDate(String fieldName)
Finds the minimum date.
|
Number |
sum(String fieldName)
Calculates the sum of a given field.
|
RealmQuery<E> |
where()
Returns a
RealmQuery , which can be used to query for specific objects from this collection. |
add, addAll, clear, containsAll, equals, hashCode, isEmpty, iterator, parallelStream, remove, removeAll, removeIf, retainAll, size, spliterator, stream, toArray, toArray
RealmQuery<E> where()
RealmQuery
, which can be used to query for specific objects from this collection.IllegalStateException
- if the Realm instance has been closed or queries are not otherwise available.RealmQuery
Number min(String fieldName)
fieldName
- the field to look for a minimum on. Only number fields are supported.null
as the value for the given field, null
will be
returned. Otherwise the minimum value is returned. When determining the minimum value, objects with null
values are ignored.IllegalArgumentException
- if the field is not a number type.IllegalStateException
- if the Realm has been closed or called from an incorrect thread.Number max(String fieldName)
fieldName
- the field to look for a maximum on. Only number fields are supported.null
as the value for the given field, null
will be
returned. Otherwise the maximum value is returned. When determining the maximum value, objects with null
values are ignored.IllegalArgumentException
- if the field is not a number type.IllegalStateException
- if the Realm has been closed or called from an incorrect thread.Number sum(String fieldName)
fieldName
- the field to sum. Only number fields are supported.null
as the value for the given field, 0
will be returned. When computing the sum, objects with null
values are ignored.IllegalArgumentException
- if the field is not a number type.IllegalStateException
- if the Realm has been closed or called from an incorrect thread.double average(String fieldName)
fieldName
- the field to calculate average on. Only number fields are supported.null
as the value for the given field,
0
will be returned. When computing the average, objects with null
values are ignored.IllegalArgumentException
- if the field is not a number type.IllegalStateException
- if the Realm has been closed or called from an incorrect thread.Date maxDate(String fieldName)
fieldName
- the field to look for the maximum date. If fieldName is not of Date type, an exception is
thrown.null
as the value for the given date field, null
will be returned. Otherwise the maximum date is returned. When determining the maximum date, objects with
null
values are ignored.IllegalArgumentException
- if fieldName is not a Date field.IllegalStateException
- if the Realm has been closed or called from an incorrect thread.Date minDate(String fieldName)
fieldName
- the field to look for the minimum date. If fieldName is not of Date type, an exception is
thrown.null
as the value for the given date field, null
will be returned. Otherwise the minimum date is returned. When determining the minimum date, objects with
null
values are ignored.IllegalArgumentException
- if fieldName is not a Date field.IllegalStateException
- if the Realm has been closed or called from an incorrect thread.boolean deleteAllFromRealm()
true
if objects was deleted, false
otherwise.IllegalStateException
- if the corresponding Realm is closed or in an incorrect thread.IllegalStateException
- if the Realm has been closed or called from an incorrect thread.boolean isLoaded()
true
if data has been loaded and is available, false
if data is still being loaded.boolean load()
true
if the data could be successfully loaded, false
otherwise.boolean isValid()
Realm
instance hasn't been closed. It
will always return true
for an unmanaged collection.true
if it is still valid to use or an unmanaged collection, false
otherwise.boolean isManaged()
If this method returns false
, the collection is unmanaged. An unmanaged collection is just a normal java
collection, so it will not be live updated.
true
if this is a managed RealmCollection
, false
otherwise.boolean contains(Object object)
Collection
contains the specified object. Returns
true
if and only if at least one element elem
in this
Collection
meets following requirement:
(object==null ? elem==null : object.equals(elem))
.contains
in interface Collection<E>
object
- the object to search for.true
if object is an element of this Collection
, false
otherwise.NullPointerException
- if the object to look for is null
and this Collection
doesn't
support null
elements.RealmCollection<E> freeze()
IllegalStateException
.
Freezing a collection also creates a Realm which has its own lifecycle, but if the live Realm that spawned the original collection is fully closed (i.e. all instances across all threads are closed), the frozen Realm and this collection will be closed as well.
Frozen collections can be queried as normal, but trying to mutate it in any way or attempting to register a listener will
throw an IllegalStateException
.
Note: Keeping a large number of frozen collections with different versions alive can have a negative impact on the filesize
of the Realm. In order to avoid such a situation, it is possible to set RealmConfiguration.Builder.maxNumberOfActiveVersions(long)
.
IllegalStateException
- if this method is called from inside a write transaction.