Class RealmSet<E>
- java.lang.Object
-
- io.realm.RealmSet<E>
-
- Type Parameters:
E- the type of the values stored in this set
- All Implemented Interfaces:
io.realm.internal.Freezable<RealmCollection<E>>,io.realm.internal.ManageableObject,RealmCollection<E>,Iterable<E>,Collection<E>,Set<E>
public class RealmSet<E> extends Object implements Set<E>, io.realm.internal.ManageableObject, RealmCollection<E>
RealmSet is a collection that contains no duplicate elements.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 aHashSet.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 Summary
Constructors Constructor Description RealmSet()Instantiates a RealmSet in unmanaged mode.RealmSet(io.realm.BaseRealm baseRealm, io.realm.internal.OsSet osSet, Class<E> valueClass)Instantiates a RealmSet in managed mode.RealmSet(io.realm.BaseRealm baseRealm, io.realm.internal.OsSet osSet, String className)Instantiates a RealmSet in managed mode.RealmSet(Collection<E> collection)Instantiates a RealmSet in unmanaged mode with another collection.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(E e)booleanaddAll(Collection<? extends E> c)voidaddChangeListener(RealmChangeListener<RealmSet<E>> listener)Adds a change listener to thisRealmSet.voidaddChangeListener(SetChangeListener<E> listener)Adds a change listener to thisRealmSet.doubleaverage(String fieldName)Returns the average of a given field.voidclear()booleancontains(Object o)Tests whether thisCollectioncontains the specified object.booleancontainsAll(Collection<?> c)booleandeleteAllFromRealm()This deletes all objects in the collection from the underlying Realm as well as from the collection.RealmSet<E>freeze()Class<E>getValueClass()StringgetValueClassName()booleanisEmpty()booleanisFrozen()booleanisLoaded()Checks if a collection has finished loading its data yet.booleanisManaged()Checks if the collection is managed by Realm.booleanisValid()Checks if the collection is still valid to use, i.e., theRealminstance hasn't been closed.Iterator<E>iterator()booleanload()Blocks the collection until all data are available.Numbermax(String fieldName)Finds the maximum value of a field.DatemaxDate(String fieldName)Finds the maximum date.Numbermin(String fieldName)Finds the minimum value of a field.DateminDate(String fieldName)Finds the minimum date.booleanremove(Object o)booleanremoveAll(Collection<?> c)voidremoveAllChangeListeners()Removes all user-defined change listeners.voidremoveChangeListener(RealmChangeListener<RealmSet<E>> listener)Removes the specified change listener.voidremoveChangeListener(SetChangeListener<E> listener)Removes the specified change listener.booleanretainAll(Collection<?> c)intsize()Numbersum(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.-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.util.Set
equals, hashCode, spliterator
-
-
-
-
Constructor Detail
-
RealmSet
public RealmSet()
Instantiates a RealmSet in unmanaged mode.
-
RealmSet
public RealmSet(Collection<E> collection)
Instantiates a RealmSet in unmanaged mode with another collection.- Parameters:
collection- the collection with which the set will be initially populated.
-
RealmSet
public RealmSet(io.realm.BaseRealm baseRealm, io.realm.internal.OsSet osSet, Class<E> valueClass)Instantiates a RealmSet in managed mode. This constructor is used internally by Realm.- Parameters:
baseRealm-osSet-valueClass-
-
RealmSet
public RealmSet(io.realm.BaseRealm baseRealm, io.realm.internal.OsSet osSet, String className)Instantiates a RealmSet in managed mode. This constructor is used internally by a Dynamic Realm.- Parameters:
baseRealm-osSet-className-
-
-
Method Detail
-
isManaged
public boolean isManaged()
Checks if the collection is managed by Realm. A managed collection is just a wrapper around the data in the underlying Realm file. On Looper threads, a managed collection will be live-updated so it always points to the latest data. Managed collections are thread confined so that they cannot be accessed from other threads than the one that created them.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.- Specified by:
isManagedin interfaceio.realm.internal.ManageableObject- Specified by:
isManagedin interfaceRealmCollection<E>- Returns:
trueif this is a managedRealmCollection,falseotherwise.
-
isValid
public boolean isValid()
Checks if the collection is still valid to use, i.e., theRealminstance hasn't been closed. It will always returntruefor an unmanaged collection.- Specified by:
isValidin interfaceio.realm.internal.ManageableObject- Specified by:
isValidin interfaceRealmCollection<E>- Returns:
trueif it is still valid to use or an unmanaged collection,falseotherwise.
-
isFrozen
public boolean isFrozen()
- Specified by:
isFrozenin interfaceio.realm.internal.ManageableObject
-
size
public int size()
-
isEmpty
public boolean isEmpty()
-
contains
public boolean contains(@Nullable Object o)
Tests whether thisCollectioncontains the specified object. Returnstrueif and only if at least one elementelemin thisCollectionmeets following requirement:(object==null ? elem==null : object.equals(elem)).- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceRealmCollection<E>- Specified by:
containsin interfaceSet<E>- Parameters:
o- the object to search for.- Returns:
trueif object is an element of thisCollection,falseotherwise.
-
toArray
public Object[] toArray()
-
toArray
public <T> T[] toArray(T[] a)
-
containsAll
public boolean containsAll(Collection<?> c)
- Specified by:
containsAllin interfaceCollection<E>- Specified by:
containsAllin interfaceSet<E>
-
addAll
public boolean addAll(Collection<? extends E> c)
-
retainAll
public boolean retainAll(Collection<?> c)
-
removeAll
public boolean removeAll(Collection<?> c)
-
clear
public void clear()
-
addChangeListener
public void addChangeListener(RealmChangeListener<RealmSet<E>> listener)
Adds a change listener to thisRealmSet.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 } }); } }- Parameters:
listener- the listener to be notified.- Throws:
IllegalArgumentException- if the change listener isnull.IllegalStateException- if you try to add a listener from a non-Looper orIntentServicethread.
-
addChangeListener
public void addChangeListener(SetChangeListener<E> listener)
Adds a change listener to thisRealmSet.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 } }); } }- Parameters:
listener- the listener to be notified.- Throws:
IllegalArgumentException- if the change listener isnull.IllegalStateException- if you try to add a listener from a non-Looper orIntentServicethread.
-
removeChangeListener
public void removeChangeListener(RealmChangeListener<RealmSet<E>> listener)
Removes the specified change listener.- Parameters:
listener- the change listener to be removed.- Throws:
IllegalArgumentException- if the change listener isnull.IllegalStateException- if you try to remove a listener from a non-Looper Thread.
-
removeChangeListener
public void removeChangeListener(SetChangeListener<E> listener)
Removes the specified change listener.- Parameters:
listener- the change listener to be removed.- Throws:
IllegalArgumentException- if the change listener isnull.IllegalStateException- if you try to remove a listener from a non-Looper Thread.
-
removeAllChangeListeners
public void removeAllChangeListeners()
Removes all user-defined change listeners.- Throws:
IllegalStateException- if you try to remove listeners from a non-Looper Thread.- See Also:
RealmChangeListener
-
where
public RealmQuery<E> where()
Returns a RealmQuery, which can be used to query for specific objects of this class.- Specified by:
wherein interfaceRealmCollection<E>- Returns:
- a RealmQuery object.
- Throws:
IllegalStateException- if Realm instance has been closed or parent object has been removed.- See Also:
RealmQuery
-
min
@Nullable public Number min(String fieldName)
Finds the minimum value of a field.- Specified by:
minin interfaceRealmCollection<E>- Parameters:
fieldName- the field to look for a minimum on. Only number fields are supported.- Returns:
- if no objects exist or they all have
nullas the value for the given field,nullwill be returned. Otherwise the minimum value is returned. When determining the minimum value, objects withnullvalues are ignored.
-
max
@Nullable public Number max(String fieldName)
Finds the maximum value of a field.- Specified by:
maxin interfaceRealmCollection<E>- Parameters:
fieldName- the field to look for a maximum on. Only number fields are supported.- Returns:
- if no objects exist or they all have
nullas the value for the given field,nullwill be returned. Otherwise the maximum value is returned. When determining the maximum value, objects withnullvalues are ignored.
-
sum
public Number sum(String fieldName)
Calculates the sum of a given field.- Specified by:
sumin interfaceRealmCollection<E>- Parameters:
fieldName- the field to sum. Only number fields are supported.- Returns:
- the sum. If no objects exist or they all have
nullas the value for the given field,0will be returned. When computing the sum, objects withnullvalues are ignored.
-
average
public double average(String fieldName)
Returns the average of a given field.- Specified by:
averagein interfaceRealmCollection<E>- Parameters:
fieldName- the field to calculate average on. Only number fields are supported.- Returns:
- the average for the given field amongst objects in query results. This will be of type double for all
types of number fields. If no objects exist or they all have
nullas the value for the given field,0will be returned. When computing the average, objects withnullvalues are ignored.
-
maxDate
@Nullable public Date maxDate(String fieldName)
Finds the maximum date.- Specified by:
maxDatein interfaceRealmCollection<E>- Parameters:
fieldName- the field to look for the maximum date. If fieldName is not of Date type, an exception is thrown.- Returns:
- if no objects exist or they all have
nullas the value for the given date field,nullwill be returned. Otherwise the maximum date is returned. When determining the maximum date, objects withnullvalues are ignored.
-
minDate
@Nullable public Date minDate(String fieldName)
Finds the minimum date.- Specified by:
minDatein interfaceRealmCollection<E>- Parameters:
fieldName- the field to look for the minimum date. If fieldName is not of Date type, an exception is thrown.- Returns:
- if no objects exist or they all have
nullas the value for the given date field,nullwill be returned. Otherwise the minimum date is returned. When determining the minimum date, objects withnullvalues are ignored.
-
deleteAllFromRealm
public boolean deleteAllFromRealm()
This deletes all objects in the collection from the underlying Realm as well as from the collection.- Specified by:
deleteAllFromRealmin interfaceRealmCollection<E>- Returns:
trueif objects was deleted,falseotherwise.
-
isLoaded
public boolean isLoaded()
Checks if a collection has finished loading its data yet.- Specified by:
isLoadedin interfaceRealmCollection<E>- Returns:
trueif data has been loaded and is available,falseif data is still being loaded.
-
load
public boolean load()
Blocks the collection until all data are available.- Specified by:
loadin interfaceRealmCollection<E>- Returns:
trueif the data could be successfully loaded,falseotherwise.
-
getValueClassName
public String getValueClassName()
-
-