Class RealmMap<K,V>
- java.lang.Object
-
- io.realm.RealmMap<K,V>
-
- Type Parameters:
K
- the type of the keys stored in this mapV
- the type of the values stored in this map
- All Implemented Interfaces:
io.realm.internal.Freezable<RealmMap<K,V>>
,io.realm.internal.ManageableObject
,Map<K,V>
- Direct Known Subclasses:
RealmDictionary
public abstract class RealmMap<K,V> extends Object implements Map<K,V>, io.realm.internal.ManageableObject, io.realm.internal.Freezable<RealmMap<K,V>>
RealmMap is used to map keys to values. A RealmMap cannot contain duplicate keys and each key can map to at most one value. A RealmMap cannot havenull
keys but can havenull
values.Similarly to
RealmList
s, a RealmDictionary can operate in managed and unmanaged modes. In managed mode a RealmDictionary persists all its contents inside a Realm whereas in unmanaged mode it functions like aHashMap
.Managed RealmDictionaries can only be created by Realm and will automatically update its content whenever the underlying Realm is updated. Managed RealmDictionaries can only be accessed using the getter that points to a RealmDictionary field of a
RealmObject
.Unmanaged RealmDictionaries can be created by the user and can contain both managed and unmanaged RealmObjects. This is useful when dealing with JSON deserializers like GSON or other frameworks that inject values into a class. Unmanaged RealmMaps can be added to a Realm using the
Realm.copyToRealm(Iterable, ImportFlag...)
method.
-
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addChangeListener(MapChangeListener<K,V> listener)
Adds a change listener to thisRealmMap
.void
addChangeListener(RealmChangeListener<RealmMap<K,V>> listener)
Adds a change listener to thisRealmMap
.void
clear()
boolean
containsKey(Object key)
boolean
containsValue(Object value)
Set<Map.Entry<K,V>>
entrySet()
RealmMap<K,V>
freeze()
V
get(Object key)
boolean
isEmpty()
boolean
isFrozen()
boolean
isManaged()
boolean
isValid()
Set<K>
keySet()
V
put(K key, V value)
void
putAll(Map<? extends K,? extends V> m)
V
remove(Object key)
void
removeAllChangeListeners()
Removes all user-defined change listeners.void
removeChangeListener(MapChangeListener<K,V> listener)
Removes the specified change listener.void
removeChangeListener(RealmChangeListener<RealmMap<K,V>> listener)
Removes the specified change listener.int
size()
Collection<V>
values()
-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Method Detail
-
isManaged
public boolean isManaged()
- Specified by:
isManaged
in interfaceio.realm.internal.ManageableObject
-
isValid
public boolean isValid()
- Specified by:
isValid
in interfaceio.realm.internal.ManageableObject
-
isFrozen
public boolean isFrozen()
- Specified by:
isFrozen
in interfaceio.realm.internal.ManageableObject
-
containsKey
public boolean containsKey(@Nullable Object key)
- Specified by:
containsKey
in interfaceMap<K,V>
-
containsValue
public boolean containsValue(@Nullable Object value)
- Specified by:
containsValue
in interfaceMap<K,V>
-
freeze
public RealmMap<K,V> freeze()
- Specified by:
freeze
in interfaceio.realm.internal.Freezable<K>
-
addChangeListener
public void addChangeListener(MapChangeListener<K,V> listener)
Adds a change listener to thisRealmMap
.Registering a change listener will not prevent the underlying RealmMap from being garbage collected. If the RealmMap 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 RealmMap<String, 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 MapChangeListener<String, Dog>() { \@Override public void onChange(RealmMap<String, Dog> map, MapChangeSet<String> changeSet) { // React to change } }); } }
- Parameters:
listener
- the change 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(RealmChangeListener<RealmMap<K,V>> listener)
Adds a change listener to thisRealmMap
.Registering a change listener will not prevent the underlying RealmMap from being garbage collected. If the RealmMap 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 RealmMap<String, 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<RealmMap<String, Dog>>() { \@Override public void onChange(RealmMap<String, Dog> map) { // React to change } }); } }
- Parameters:
listener
- the change 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.- See Also:
RealmChangeListener
-
removeChangeListener
public void removeChangeListener(MapChangeListener<K,V> 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(RealmChangeListener<RealmMap<K,V>> 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.- See Also:
RealmChangeListener
-
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
-
-