E - the type of the values stored in this setpublic class RealmSet<E> extends Object implements Set<E>, RealmCollection<E>
Similarly to RealmLists, a RealmSet can operate in managed and unmanaged modes. In
managed mode a RealmSet persists all its contents inside a Realm whereas in unmanaged mode
it functions like a HashSet.
Managed RealmSets can only be created by Realm and will automatically update its content
whenever the underlying Realm is updated. Managed RealmSet can only be accessed using the getter
that points to a RealmSet field of a RealmObject.
Unmanaged elements in this set can be added to a Realm using the
Realm.copyToRealm(Iterable, ImportFlag...) method.
Warning: the following methods are not supported for classes containing set fields yet:
Realm.insert(RealmModel)Realm.insert(Collection)Realm.insertOrUpdate(RealmModel)Realm.insertOrUpdate(Collection)Realm.createAllFromJson(Class, String)Realm.createAllFromJson(Class, JSONArray)Realm.createAllFromJson(Class, InputStream)Realm.createObjectFromJson(Class, String)Realm.createObjectFromJson(Class, JSONObject)}Realm.createObjectFromJson(Class, InputStream)}Realm.createOrUpdateAllFromJson(Class, String)Realm.createOrUpdateAllFromJson(Class, JSONArray)Realm.createOrUpdateAllFromJson(Class, InputStream)Realm.createOrUpdateObjectFromJson(Class, String)Realm.createOrUpdateObjectFromJson(Class, JSONObject)Realm.createOrUpdateObjectFromJson(Class, InputStream)| Constructor and Description |
|---|
RealmSet()
Instantiates a RealmSet in unmanaged mode.
|
RealmSet(io.realm.BaseRealm baseRealm,
OsSet osSet,
Class<E> valueClass)
Instantiates a RealmSet in managed mode.
|
RealmSet(io.realm.BaseRealm baseRealm,
OsSet osSet,
String className)
Instantiates a RealmSet in managed mode.
|
RealmSet(Collection<E> collection)
Instantiates a RealmSet in unmanaged mode with another collection.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E e) |
boolean |
addAll(Collection<? extends E> c) |
void |
addChangeListener(RealmChangeListener<RealmSet<E>> listener)
Adds a change listener to this
RealmSet. |
void |
addChangeListener(SetChangeListener<E> listener)
Adds a change listener to this
RealmSet. |
double |
average(String fieldName)
Returns the average of a given field.
|
void |
clear() |
boolean |
contains(Object o)
Tests whether this
Collection contains the specified object. |
boolean |
containsAll(Collection<?> c) |
boolean |
deleteAllFromRealm()
This deletes all objects in the collection from the underlying Realm as well as from the collection.
|
RealmSet<E> |
freeze() |
Class<E> |
getValueClass() |
String |
getValueClassName() |
boolean |
isEmpty() |
boolean |
isFrozen() |
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. |
Iterator<E> |
iterator() |
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.
|
boolean |
remove(Object o) |
boolean |
removeAll(Collection<?> c) |
void |
removeAllChangeListeners()
Removes all user-defined change listeners.
|
void |
removeChangeListener(RealmChangeListener<RealmSet<E>> listener)
Removes the specified change listener.
|
void |
removeChangeListener(SetChangeListener<E> listener)
Removes the specified change listener.
|
boolean |
retainAll(Collection<?> c) |
int |
size() |
Number |
sum(String fieldName)
Calculates the sum of a given field.
|
Object[] |
toArray() |
<T> T[] |
toArray(T[] a) |
RealmQuery<E> |
where()
Returns a RealmQuery, which can be used to query for specific objects of this class.
|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitequals, hashCode, spliteratorparallelStream, removeIf, streampublic RealmSet()
public RealmSet(Collection<E> collection)
collection - the collection with which the set will be initially populated.public RealmSet(io.realm.BaseRealm baseRealm,
OsSet osSet,
Class<E> valueClass)
baseRealm - osSet - valueClass - public RealmSet(io.realm.BaseRealm baseRealm,
OsSet osSet,
String className)
baseRealm - osSet - className - public 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.
isManaged in interface RealmCollection<E>true if this is a managed RealmCollection, false otherwise.public boolean isValid()
Realm instance hasn't been closed. It
will always return true for an unmanaged collection.isValid in interface RealmCollection<E>true if it is still valid to use or an unmanaged collection, false otherwise.public boolean isFrozen()
public int size()
public boolean isEmpty()
public boolean contains(Object o)
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 RealmCollection<E>contains in interface Collection<E>contains in interface Set<E>o - the object to search for.true if object is an element of this Collection, false otherwise.public Object[] toArray()
public <T> T[] toArray(T[] a)
public boolean add(E e)
public boolean remove(Object o)
public boolean containsAll(Collection<?> c)
containsAll in interface Collection<E>containsAll in interface Set<E>public boolean addAll(Collection<? extends E> c)
public boolean retainAll(Collection<?> c)
public boolean removeAll(Collection<?> c)
public void clear()
public void addChangeListener(RealmChangeListener<RealmSet<E>> listener)
RealmSet.
Registering a change listener will not prevent the underlying RealmSet from being garbage collected. If the RealmSet is garbage collected, the change listener will stop being triggered. To avoid this, keep a strong reference for as long as appropriate e.g. in a class variable.
public class MyActivity extends Activity {
private RealmSet<Dog> dogs; // Strong reference to keep listeners alive
\@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dogs = realm.where(Person.class).findFirst().getDogs();
dogs.addChangeListener(new RealmChangeListener<RealmSet<Dog>>() {
\@Override
public void onChange(RealmSet<Dog> map) {
// React to change
}
});
}
}
listener - the listener to be notified.IllegalArgumentException - if the change listener is null.IllegalStateException - if you try to add a listener from a non-Looper or
IntentService thread.public void addChangeListener(SetChangeListener<E> listener)
RealmSet.
Registering a change listener will not prevent the underlying RealmSet from being garbage collected. If the RealmSet is garbage collected, the change listener will stop being triggered. To avoid this, keep a strong reference for as long as appropriate e.g. in a class variable.
public class MyActivity extends Activity {
private RealmSet<Dog> dogs; // Strong reference to keep listeners alive
\@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dogs = realm.where(Person.class).findFirst().getDogs();
dogs.addChangeListener(new SetChangeListener<Dog>() {
\@Override
public void onChange(RealmSet<Dog> set, SetChangeSet changeSet) {
// React to change
}
});
}
}
listener - the listener to be notified.IllegalArgumentException - if the change listener is null.IllegalStateException - if you try to add a listener from a non-Looper or
IntentService thread.public void removeChangeListener(RealmChangeListener<RealmSet<E>> listener)
listener - the change listener to be removed.IllegalArgumentException - if the change listener is null.IllegalStateException - if you try to remove a listener from a non-Looper Thread.public void removeChangeListener(SetChangeListener<E> listener)
listener - the change listener to be removed.IllegalArgumentException - if the change listener is null.IllegalStateException - if you try to remove a listener from a non-Looper Thread.public void removeAllChangeListeners()
IllegalStateException - if you try to remove listeners from a non-Looper Thread.RealmChangeListenerpublic RealmQuery<E> where()
where in interface RealmCollection<E>IllegalStateException - if Realm instance has been closed or parent object has been removed.RealmQuerypublic Number min(String fieldName)
min in interface RealmCollection<E>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 Number max(String fieldName)
max in interface RealmCollection<E>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 Number sum(String fieldName)
sum in interface RealmCollection<E>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(String fieldName)
average in interface RealmCollection<E>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 Date maxDate(String fieldName)
maxDate in interface RealmCollection<E>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 Date minDate(String fieldName)
minDate in interface RealmCollection<E>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>true if objects was deleted, false otherwise.public boolean isLoaded()
isLoaded in interface RealmCollection<E>true if data has been loaded and is available, false if data is still being loaded.public boolean load()
load in interface RealmCollection<E>true if the data could be successfully loaded, false otherwise.public String getValueClassName()