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
RealmList
s, 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 boolean
add(E e)
boolean
addAll(Collection<? extends E> c)
void
addChangeListener(RealmChangeListener<RealmSet<E>> listener)
Adds a change listener to thisRealmSet
.void
addChangeListener(SetChangeListener<E> listener)
Adds a change listener to thisRealmSet
.double
average(String fieldName)
Returns the average of a given field.void
clear()
boolean
contains(Object o)
Tests whether thisCollection
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., theRealm
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.-
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:
isManaged
in interfaceio.realm.internal.ManageableObject
- Specified by:
isManaged
in interfaceRealmCollection<E>
- Returns:
true
if this is a managedRealmCollection
,false
otherwise.
-
isValid
public boolean isValid()
Checks if the collection is still valid to use, i.e., theRealm
instance hasn't been closed. It will always returntrue
for an unmanaged collection.- Specified by:
isValid
in interfaceio.realm.internal.ManageableObject
- Specified by:
isValid
in interfaceRealmCollection<E>
- Returns:
true
if it is still valid to use or an unmanaged collection,false
otherwise.
-
isFrozen
public boolean isFrozen()
- Specified by:
isFrozen
in interfaceio.realm.internal.ManageableObject
-
size
public int size()
-
isEmpty
public boolean isEmpty()
-
contains
public boolean contains(@Nullable Object o)
Tests whether thisCollection
contains the specified object. Returnstrue
if and only if at least one elementelem
in thisCollection
meets following requirement:(object==null ? elem==null : object.equals(elem))
.- Specified by:
contains
in interfaceCollection<E>
- Specified by:
contains
in interfaceRealmCollection<E>
- Specified by:
contains
in interfaceSet<E>
- Parameters:
o
- the object to search for.- Returns:
true
if object is an element of thisCollection
,false
otherwise.
-
toArray
public Object[] toArray()
-
toArray
public <T> T[] toArray(T[] a)
-
containsAll
public boolean containsAll(Collection<?> c)
- Specified by:
containsAll
in interfaceCollection<E>
- Specified by:
containsAll
in 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 orIntentService
thread.
-
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 orIntentService
thread.
-
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:
where
in 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:
min
in 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
null
as the value for the given field,null
will be returned. Otherwise the minimum value is returned. When determining the minimum value, objects withnull
values are ignored.
-
max
@Nullable public Number max(String fieldName)
Finds the maximum value of a field.- Specified by:
max
in 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
null
as the value for the given field,null
will be returned. Otherwise the maximum value is returned. When determining the maximum value, objects withnull
values are ignored.
-
sum
public Number sum(String fieldName)
Calculates the sum of a given field.- Specified by:
sum
in interfaceRealmCollection<E>
- Parameters:
fieldName
- the field to sum. Only number fields are supported.- Returns:
- the sum. If no objects exist or they all have
null
as the value for the given field,0
will be returned. When computing the sum, objects withnull
values are ignored.
-
average
public double average(String fieldName)
Returns the average of a given field.- Specified by:
average
in 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
null
as the value for the given field,0
will be returned. When computing the average, objects withnull
values are ignored.
-
maxDate
@Nullable public Date maxDate(String fieldName)
Finds the maximum date.- Specified by:
maxDate
in 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
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 withnull
values are ignored.
-
minDate
@Nullable public Date minDate(String fieldName)
Finds the minimum date.- Specified by:
minDate
in 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
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 withnull
values 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:
deleteAllFromRealm
in interfaceRealmCollection<E>
- Returns:
true
if objects was deleted,false
otherwise.
-
isLoaded
public boolean isLoaded()
Checks if a collection has finished loading its data yet.- Specified by:
isLoaded
in interfaceRealmCollection<E>
- Returns:
true
if data has been loaded and is available,false
if data is still being loaded.
-
load
public boolean load()
Blocks the collection until all data are available.- Specified by:
load
in interfaceRealmCollection<E>
- Returns:
true
if the data could be successfully loaded,false
otherwise.
-
getValueClassName
public String getValueClassName()
-
-