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 havenullkeys but can havenullvalues.Similarly to
RealmLists, 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 voidaddChangeListener(MapChangeListener<K,V> listener)Adds a change listener to thisRealmMap.voidaddChangeListener(RealmChangeListener<RealmMap<K,V>> listener)Adds a change listener to thisRealmMap.voidclear()booleancontainsKey(Object key)booleancontainsValue(Object value)Set<Map.Entry<K,V>>entrySet()RealmMap<K,V>freeze()Vget(Object key)booleanisEmpty()booleanisFrozen()booleanisManaged()booleanisValid()Set<K>keySet()Vput(K key, V value)voidputAll(Map<? extends K,? extends V> m)Vremove(Object key)voidremoveAllChangeListeners()Removes all user-defined change listeners.voidremoveChangeListener(MapChangeListener<K,V> listener)Removes the specified change listener.voidremoveChangeListener(RealmChangeListener<RealmMap<K,V>> listener)Removes the specified change listener.intsize()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:
isManagedin interfaceio.realm.internal.ManageableObject
-
isValid
public boolean isValid()
- Specified by:
isValidin interfaceio.realm.internal.ManageableObject
-
isFrozen
public boolean isFrozen()
- Specified by:
isFrozenin interfaceio.realm.internal.ManageableObject
-
containsKey
public boolean containsKey(@Nullable Object key)
- Specified by:
containsKeyin interfaceMap<K,V>
-
containsValue
public boolean containsValue(@Nullable Object value)
- Specified by:
containsValuein interfaceMap<K,V>
-
freeze
public RealmMap<K,V> freeze()
- Specified by:
freezein 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 orIntentServicethread.
-
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 orIntentServicethread.- 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
-
-