K - the type of the keys stored in this mapV - the type of the values stored in this mappublic abstract class RealmMap<K,V> extends Object implements Map<K,V>
null keys but can have null
values.
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 a HashMap.
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.
| Modifier and Type | Method and Description |
|---|---|
void |
addChangeListener(MapChangeListener<K,V> listener)
Adds a change listener to this
RealmMap. |
void |
addChangeListener(RealmChangeListener<RealmMap<K,V>> listener)
Adds a change listener to this
RealmMap. |
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() |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcompute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAllpublic boolean isManaged()
public boolean isValid()
public boolean isFrozen()
public boolean containsKey(Object key)
containsKey in interface Map<K,V>public boolean containsValue(Object value)
containsValue in interface Map<K,V>public void addChangeListener(MapChangeListener<K,V> listener)
RealmMap.
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
}
});
}
}
listener - the change 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(RealmChangeListener<RealmMap<K,V>> listener)
RealmMap.
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
}
});
}
}
listener - the change 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.RealmChangeListenerpublic void removeChangeListener(MapChangeListener<K,V> 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(RealmChangeListener<RealmMap<K,V>> 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.RealmChangeListenerpublic void removeAllChangeListeners()
IllegalStateException - if you try to remove listeners from a non-Looper Thread.RealmChangeListener