E
- the class of objects in list.public class RealmList<E extends RealmObject>
extends java.util.AbstractList<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.
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.
|
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.
|
void |
deleteAllFromRealm()
Removes all elements from this list and delete them from the corresponding Realm.
|
E |
first()
Finds the first object.
|
E |
get(int location)
Returns the element at the specified location in this list.
|
boolean |
isValid()
Checks if
RealmResults is still valid to use i.e. |
E |
last()
Finds the last object.
|
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.
|
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 . |
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, iterator, lastIndexOf, listIterator, listIterator, subList
addAll, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toArray
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()
RealmResults
is still valid to use i.e. the Realm
instance hasn't
been closed.true
if still valid to use, false
otherwise or if it is a standalone object.public void add(int location, E object)
Realm.copyToRealm(RealmObject)
afterwards.Realm.copyToRealm(RealmObject)
or Realm.copyToRealmOrUpdate(RealmObject)
if it has a primary key.add
in interface java.util.List<E extends RealmObject>
add
in class java.util.AbstractList<E extends RealmObject>
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(RealmObject)
afterwards.Realm.copyToRealm(RealmObject)
or Realm.copyToRealmOrUpdate(RealmObject)
if it has a primary key.add
in interface java.util.Collection<E extends RealmObject>
add
in interface java.util.List<E extends RealmObject>
add
in class java.util.AbstractList<E extends RealmObject>
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(RealmObject)
afterwards.Realm.copyToRealm(RealmObject)
or
Realm.copyToRealmOrUpdate(RealmObject)
if it has a primary key.set
in interface java.util.List<E extends RealmObject>
set
in class java.util.AbstractList<E extends RealmObject>
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 RealmObject>
clear
in interface java.util.List<E extends RealmObject>
clear
in class java.util.AbstractList<E extends RealmObject>
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 RealmObject>
remove
in class java.util.AbstractList<E extends RealmObject>
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 void deleteAllFromRealm()
RealmList
if all of the RealmObjects in the list are managed by Realm.java.lang.IllegalStateException
- if the Realm instance has been closed, the parent object has been removed, the
method is called in a wrong thread or any RealmObject in the list is not managed by Realm.clear()
public E get(int location)
get
in interface java.util.List<E extends RealmObject>
get
in class java.util.AbstractList<E extends RealmObject>
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()
null
if the list is empty.java.lang.IllegalStateException
- if Realm instance has been closed or parent object has been removed.public E last()
null
if the list is empty.java.lang.IllegalStateException
- if Realm instance has been closed or parent object has been removed.public int size()
List
.size
in interface java.util.Collection<E extends RealmObject>
size
in interface java.util.List<E extends RealmObject>
size
in class java.util.AbstractCollection<E extends RealmObject>
List
.java.lang.IllegalStateException
- if Realm instance has been closed or parent object has been removed.public RealmQuery<E> where()
java.lang.IllegalStateException
- if Realm instance has been closed or parent object has been removed.RealmQuery
public boolean contains(java.lang.Object object)
List.contains(Object)
implementation will occur.contains
in interface java.util.Collection<E extends RealmObject>
contains
in interface java.util.List<E extends RealmObject>
contains
in class java.util.AbstractCollection<E extends RealmObject>
object
- the element whose presence in this list is to be tested.true
if this list contains the specified element otherwise false
.public java.lang.String toString()
toString
in class java.util.AbstractCollection<E extends RealmObject>