E
- the class of objects in list.public final class RealmList<E extends RealmModel> extends java.util.AbstractList<E> implements OrderedRealmCollection<E>
RealmObject
.
RealmList has two modes: A managed and non-managed mode. In managed mode all objects are persisted inside a Realm, in
non-managed mode it works as a normal ArrayList.
Only Realm can create managed RealmLists. Managed RealmLists will automatically update the content whenever the
underlying Realm is updated, and can only be accessed using the getter of a RealmObject
.
Non-managed RealmLists can be created by the user and can contain both managed and non-managed RealmObjects. This is
useful when dealing with JSON deserializers like GSON or other frameworks that inject values into a class.
Non-managed elements in this list can be added to a Realm using the Realm.copyToRealm(Iterable)
method.
RealmList
can contain more elements than Integer.MAX_VALUE
.
In that case, you can access only first Integer.MAX_VALUE
elements in it.
Modifier and Type | Field and Description |
---|---|
static java.lang.String |
REMOVE_OUTSIDE_TRANSACTION_ERROR |
Constructor and Description |
---|
RealmList()
Creates a RealmList in non-managed mode, where the elements are not controlled by a Realm.
|
RealmList(E... objects)
Creates a RealmList in non-managed mode with an initial list of elements.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E object)
Adds the specified object at the end of this List.
|
void |
add(int location,
E object)
Inserts the specified object into this List at the specified location.
|
double |
average(java.lang.String fieldName)
Returns the average of a given field.
|
void |
clear()
Removes all elements from this list, leaving it empty.
|
boolean |
contains(java.lang.Object object)
Returns true if the list contains the specified element when attached to a Realm.
|
boolean |
deleteAllFromRealm()
This deletes all objects in the collection from the underlying Realm as well as from the collection.
|
boolean |
deleteFirstFromRealm()
Deletes the first object from the Realm.
|
void |
deleteFromRealm(int location)
Deletes the object at the given index from the Realm.
|
boolean |
deleteLastFromRealm()
Deletes the last object from the Realm.
|
E |
first()
Finds the first object.
|
E |
get(int location)
Returns the element at the specified location in this list.
|
boolean |
isLoaded()
Checks if a collection has finished loading its data yet.
|
boolean |
isValid()
Checks if the
RealmList is managed by Realm and contains valid data i.e. |
java.util.Iterator<E> |
iterator() |
E |
last()
Finds the last object.
|
java.util.ListIterator<E> |
listIterator() |
java.util.ListIterator<E> |
listIterator(int location) |
boolean |
load()
Blocks the collection until all data are available.
|
java.lang.Number |
max(java.lang.String fieldName)
Finds the maximum value of a field.
|
java.util.Date |
maxDate(java.lang.String fieldName)
Finds the maximum date.
|
java.lang.Number |
min(java.lang.String fieldName)
Finds the minimum value of a field.
|
java.util.Date |
minDate(java.lang.String fieldName)
Finds the minimum date.
|
void |
move(int oldPos,
int newPos)
Moves an object from one position to another, while maintaining a fixed sized list.
|
E |
remove(int location)
Removes the object at the specified location from this list.
|
boolean |
remove(java.lang.Object object)
Removes one instance of the specified object from this
Collection if one
is contained . |
boolean |
removeAll(java.util.Collection<?> collection)
Removes all occurrences in this
Collection of each object in the
specified Collection . |
E |
set(int location,
E object)
Replaces the element at the specified location in this list with the specified object.
|
int |
size()
Returns the number of elements in this
List . |
RealmResults<E> |
sort(java.lang.String fieldName)
Sorts a collection based on the provided field in ascending order.
|
RealmResults<E> |
sort(java.lang.String[] fieldNames,
Sort[] sortOrders)
Sorts a collection based on the provided fields and sort orders.
|
RealmResults<E> |
sort(java.lang.String fieldName,
Sort sortOrder)
Sorts a collection based on the provided field and sort order.
|
RealmResults<E> |
sort(java.lang.String fieldName1,
Sort sortOrder1,
java.lang.String fieldName2,
Sort sortOrder2)
Sorts a collection based on the provided fields and sort orders.
|
java.lang.Number |
sum(java.lang.String fieldName)
Calculates the sum of a given field.
|
java.lang.String |
toString() |
RealmQuery<E> |
where()
Returns a RealmQuery, which can be used to query for specific objects of this class.
|
addAll, equals, hashCode, indexOf, lastIndexOf, subList
addAll, containsAll, isEmpty, retainAll, toArray, toArray
public static final java.lang.String REMOVE_OUTSIDE_TRANSACTION_ERROR
public RealmList()
ArrayList
and it is not possible to query
the objects in this state.
Use Realm.copyToRealm(Iterable)
to properly persist it's elements in Realm.
public RealmList(E... objects)
ArrayList
and it is not possible to query the
objects in this state.
Use Realm.copyToRealm(Iterable)
to properly persist all non-managed elements in Realm.objects
- initial objects in the list.public boolean isValid()
RealmList
is managed by Realm and contains valid data i.e. the Realm
instance hasn't been closed.isValid
in interface RealmCollection<E extends RealmModel>
true
if still valid to use, false
otherwise or if it's an un-managed list.public void add(int location, E object)
Realm.copyToRealm(RealmModel)
afterwards.Realm.copyToRealm(RealmModel)
or Realm.copyToRealmOrUpdate(RealmModel)
if it has a primary key.add
in interface java.util.List<E extends RealmModel>
add
in class java.util.AbstractList<E extends RealmModel>
location
- the index at which to insert.object
- the object to add.java.lang.IllegalStateException
- if Realm instance has been closed or container object has been removed.java.lang.IndexOutOfBoundsException
- if location < 0 || location > size()
.public boolean add(E object)
Realm.copyToRealm(RealmModel)
afterwards.Realm.copyToRealm(RealmModel)
or Realm.copyToRealmOrUpdate(RealmModel)
if it has a primary key.add
in interface java.util.Collection<E extends RealmModel>
add
in interface java.util.List<E extends RealmModel>
add
in class java.util.AbstractList<E extends RealmModel>
object
- the object to add.java.lang.IllegalStateException
- if Realm instance has been closed or parent object has been removed.public E set(int location, E object)
Realm.copyToRealm(RealmModel)
afterwards.Realm.copyToRealm(RealmModel)
or
Realm.copyToRealmOrUpdate(RealmModel)
if it has a primary key.set
in interface java.util.List<E extends RealmModel>
set
in class java.util.AbstractList<E extends RealmModel>
location
- the index at which to put the specified object.object
- the object to add.java.lang.IllegalStateException
- if Realm instance has been closed or parent object has been removed.java.lang.IndexOutOfBoundsException
- if location < 0 || location >= size()
.public void move(int oldPos, int newPos)
oldPos
- index of RealmObject to move.newPos
- target position. If newPos < oldPos the object at the location will be shifted to the right. If
oldPos < newPos, indexes > oldPos will be shifted once to the left.java.lang.IllegalStateException
- if Realm instance has been closed or parent object has been removed.java.lang.IndexOutOfBoundsException
- if any position is outside [0, size()].public void clear()
clear
in interface java.util.Collection<E extends RealmModel>
clear
in interface java.util.List<E extends RealmModel>
clear
in class java.util.AbstractList<E extends RealmModel>
java.lang.IllegalStateException
- if Realm instance has been closed or parent object has been removed.List.isEmpty()
,
List.size()
,
deleteAllFromRealm()
public E remove(int location)
remove
in interface java.util.List<E extends RealmModel>
remove
in class java.util.AbstractList<E extends RealmModel>
location
- the index of the object to remove.java.lang.IllegalStateException
- if Realm instance has been closed or parent object has been removed.java.lang.IndexOutOfBoundsException
- if location < 0 || location >= size()
.public boolean remove(java.lang.Object object)
Collection
if one
is contained . This implementation iterates over this
Collection
and tests for each element e
returned by the iterator,
whether e
is equal to the given object. If object != null
then this test is performed using object.equals(e)
, otherwise
using object == null
. If an element equal to the given object is
found, then the remove
method is called on the iterator and
true
is returned, false
otherwise. If the iterator does
not support removing elements, an UnsupportedOperationException
is thrown.remove
in interface java.util.Collection<E extends RealmModel>
remove
in interface java.util.List<E extends RealmModel>
remove
in class java.util.AbstractCollection<E extends RealmModel>
object
- the object to remove.true
if this Collection
is modified, false
otherwise.java.lang.ClassCastException
- if the object passed is not of the correct type.java.lang.NullPointerException
- if object
is null
.public boolean removeAll(java.util.Collection<?> collection)
Collection
of each object in the
specified Collection
. After this method returns none of the
elements in the passed Collection
can be found in this Collection
anymore.
This implementation iterates over this Collection
and tests for each
element e
returned by the iterator, whether it is contained in
the specified Collection
. If this test is positive, then the remove
method is called on the iterator.
removeAll
in interface java.util.Collection<E extends RealmModel>
removeAll
in interface java.util.List<E extends RealmModel>
removeAll
in class java.util.AbstractCollection<E extends RealmModel>
collection
- the collection of objects to remove.true
if this Collection
is modified, false
otherwise.java.lang.ClassCastException
- if one or more elements of collection
isn't of the correct type.java.lang.NullPointerException
- if collection
is null
.public boolean deleteFirstFromRealm()
deleteFirstFromRealm
in interface OrderedRealmCollection<E extends RealmModel>
true
if an object was deleted, false
otherwise.public boolean deleteLastFromRealm()
deleteLastFromRealm
in interface OrderedRealmCollection<E extends RealmModel>
true
if an object was deleted, false
otherwise.public E get(int location)
get
in interface java.util.List<E extends RealmModel>
get
in class java.util.AbstractList<E extends RealmModel>
location
- the index of the element to return.java.lang.IllegalStateException
- if Realm instance has been closed or parent object has been removed.java.lang.IndexOutOfBoundsException
- if location < 0 || location >= size()
.public E first()
first
in interface OrderedRealmCollection<E extends RealmModel>
null
if the list is empty.java.lang.IllegalStateException
- if Realm instance has been closed or parent object has been removed.public E last()
last
in interface OrderedRealmCollection<E extends RealmModel>
null
if the list is empty.java.lang.IllegalStateException
- if Realm instance has been closed or parent object has been removed.public RealmResults<E> sort(java.lang.String fieldName)
sort
in interface OrderedRealmCollection<E extends RealmModel>
fieldName
- the field name to sort by. Only fields of type boolean, short, int, long, float, double, Date,
and String are supported.public RealmResults<E> sort(java.lang.String fieldName, Sort sortOrder)
sort
in interface OrderedRealmCollection<E extends RealmModel>
fieldName
- the field name to sort by. Only fields of type boolean, short, int, long, float, double, Date,
and String are supported.sortOrder
- the direction to sort by.public RealmResults<E> sort(java.lang.String fieldName1, Sort sortOrder1, java.lang.String fieldName2, Sort sortOrder2)
sort
in interface OrderedRealmCollection<E extends RealmModel>
fieldName1
- first field name. Only fields of type boolean, short, int, long, float,
double, Date, and String are supported.sortOrder1
- sort order for first field.fieldName2
- second field name. Only fields of type boolean, short, int, long, float,
double, Date, and String are supported.sortOrder2
- sort order for second field.public RealmResults<E> sort(java.lang.String[] fieldNames, Sort[] sortOrders)
sort
in interface OrderedRealmCollection<E extends RealmModel>
fieldNames
- an array of field names to sort by. Only fields of type boolean, short, int, long, float,
double, Date, and String are supported.sortOrders
- the directions to sort by.public void deleteFromRealm(int location)
deleteFromRealm
in interface OrderedRealmCollection<E extends RealmModel>
location
- the array index identifying the object to be removed.public int size()
List
.size
in interface java.util.Collection<E extends RealmModel>
size
in interface java.util.List<E extends RealmModel>
size
in class java.util.AbstractCollection<E extends RealmModel>
List
.java.lang.IllegalStateException
- if Realm instance has been closed or parent object has been removed.public RealmQuery<E> where()
where
in interface RealmCollection<E extends RealmModel>
java.lang.IllegalStateException
- if Realm instance has been closed or parent object has been removed.RealmQuery
public java.lang.Number min(java.lang.String fieldName)
min
in interface RealmCollection<E extends RealmModel>
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.public java.lang.Number max(java.lang.String fieldName)
max
in interface RealmCollection<E extends RealmModel>
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.public java.lang.Number sum(java.lang.String fieldName)
sum
in interface RealmCollection<E extends RealmModel>
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.public double average(java.lang.String fieldName)
average
in interface RealmCollection<E extends RealmModel>
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.public java.util.Date maxDate(java.lang.String fieldName)
maxDate
in interface RealmCollection<E extends RealmModel>
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.public java.util.Date minDate(java.lang.String fieldName)
minDate
in interface RealmCollection<E extends RealmModel>
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.public boolean deleteAllFromRealm()
deleteAllFromRealm
in interface RealmCollection<E extends RealmModel>
true
if objects was deleted, false
otherwise.public boolean isLoaded()
isLoaded
in interface RealmCollection<E extends RealmModel>
true
if data has been loaded and is available, false
if data is still being loaded.public boolean load()
load
in interface RealmCollection<E extends RealmModel>
true
if the data could be successfully loaded, false
otherwise.public boolean contains(java.lang.Object object)
List.contains(Object)
implementation will occur.contains
in interface RealmCollection<E extends RealmModel>
contains
in interface java.util.Collection<E extends RealmModel>
contains
in interface java.util.List<E extends RealmModel>
contains
in class java.util.AbstractCollection<E extends RealmModel>
object
- the element whose presence in this list is to be tested.true
if this list contains the specified element otherwise false
.public java.util.Iterator<E> iterator()
iterator
in interface java.lang.Iterable<E extends RealmModel>
iterator
in interface java.util.Collection<E extends RealmModel>
iterator
in interface java.util.List<E extends RealmModel>
iterator
in class java.util.AbstractList<E extends RealmModel>
public java.util.ListIterator<E> listIterator()
listIterator
in interface java.util.List<E extends RealmModel>
listIterator
in class java.util.AbstractList<E extends RealmModel>
public java.util.ListIterator<E> listIterator(int location)
listIterator
in interface java.util.List<E extends RealmModel>
listIterator
in class java.util.AbstractList<E extends RealmModel>
public java.lang.String toString()
toString
in class java.util.AbstractCollection<E extends RealmModel>