A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.mongodb.com/docs/atlas/device-sdks/sdk/java/model-data/data-types/realmdictionary/ below:

RealmDictionary - Java SDK - Atlas Device SDKs

New in version 10.6.0.

You can use the RealmDictionary data type to manage a collection of unique String keys paired with values. RealmDictionary implements Java's Map interface, so it works just like the built-in HashMap class, except managed RealmDictionary instances persist their contents to a realm. RealmDictionary instances that contain Realm objects store references to those objects. When you delete a Realm object from a realm, any references to that object in a RealmDictionary become null values.

To create a field of type RealmDictionary, define an object property of type RealmDictionary<T>, where T defines the values you would like to store in your RealmDictionary. Currently, RealmDictionary instances can only use keys of type String.

The following table shows which methods you can use to complete common collection tasks with RealmDictionary:

Task

Method

Add an object to a RealmDictionary

put() (or the [] operator in Kotlin)

Add multiple objects to a RealmDictionary

putAll()

Check if the dictionary contains an specific key

containsKey()

Check if the dictionary contains a specific value

containsValue()

import io.realm.RealmDictionary;import io.realm.RealmObject;public class Frog extends RealmObject {    String name;    RealmDictionary<Frog> nicknamesToFriends;        public Frog() {}    public String getName() { return name; }    public void setName(String name) { this.name = name; }    public RealmDictionary<Frog> getNicknamesToFriends() { return nicknamesToFriends; }    public void setNicknamesToFriends(RealmDictionary<Frog> nicknamesToFriends) { this.nicknamesToFriends = nicknamesToFriends; }}
Frog frog = realm.createObject(Frog.class);frog.setName("George Washington");RealmDictionary<Frog> dictionary = frog.getNicknamesToFriends();Frog wirt = realm.createObject(Frog.class);wirt.setName("Wirt");dictionary.put("tall frog", wirt);Frog greg = realm.createObject(Frog.class);greg.setName("Greg");Frog beatrice = realm.createObject(Frog.class);beatrice.setName("Beatrice");dictionary.putAll(Map.of("small frog", greg, "feathered frog", beatrice));Assert.assertTrue(dictionary.containsKey("small frog"));Assert.assertTrue(dictionary.containsValue(greg));dictionary.remove("feathered frog");Assert.assertFalse(dictionary.containsKey("feathered frog"));int sizeOfDictionaryBeforeDelete = dictionary.size();greg.deleteFromRealm();Assert.assertEquals(sizeOfDictionaryBeforeDelete, dictionary.size());Assert.assertEquals(dictionary.get("small frog"), null);
import io.realm.RealmDictionaryimport io.realm.RealmObjectopen class Frog    : RealmObject() {    var name: String? = null    var nicknamesToFriends: RealmDictionary<Frog> = RealmDictionary<Frog>()}
val frog =    realm.createObject(Frog::class.java)frog.name = "George Washington"val dictionary = frog.nicknamesToFriendsval wirt =    realm.createObject(Frog::class.java)wirt.name = "Wirt"dictionary["tall frog"] = wirtval greg =    realm.createObject(Frog::class.java)greg.name = "Greg"val beatrice =    realm.createObject(Frog::class.java)beatrice.name = "Beatrice"dictionary.putAll(mapOf<String, Frog>(    Pair("small frog", greg),    Pair("feathered frog", beatrice)))Assert.assertTrue(dictionary.containsKey("small frog"))Assert.assertTrue(dictionary.containsValue(greg))dictionary.remove("feathered frog")Assert.assertFalse(dictionary.containsKey("feathered frog"))val sizeOfDictionaryBeforeDelete = dictionary.sizegreg.deleteFromRealm()Assert.assertEquals(    sizeOfDictionaryBeforeDelete.toLong(),    dictionary.size.toLong())Assert.assertEquals(dictionary["small frog"], null)

To subscribe to changes to a RealmDictionary, pass a MapChangeListener implementation to the RealmSet.addChangeListener method. Your MapChangeListener implementation must define an onChange() method, which accepts a reference to the changed RealmDictionary and a set of changes as parameters. You can access the keys added to the dictionary as well as the keys removed from the dictionary through the MapChangeSet parameter.

AtomicReference<Frog> frog = new AtomicReference<Frog>();realm.executeTransaction(r -> {    frog.set(realm.createObject(Frog.class));    frog.get().setName("Jonathan Livingston Applesauce");});MapChangeListener<String, Frog> mapChangeListener =    new MapChangeListener<String, Frog>() {        @Override        public void onChange(RealmMap<String, Frog> map, MapChangeSet<String> changes) {            for (String insertion : changes.getInsertions()) {                Log.v("EXAMPLE",                        "Inserted key: " + insertion +                                ", Inserted value: " + map.get(insertion).getName());            }        }    };frog.get().getNicknamesToFriends().addChangeListener(mapChangeListener);realm.executeTransaction(r -> {        RealmDictionary<Frog> dictionary = frog.get().getNicknamesToFriends();        Frog wirt = realm.createObject(Frog.class);    wirt.setName("Wirt");    dictionary.put("tall frog", wirt);        Frog greg = realm.createObject(Frog.class);    greg.setName("Greg");    Frog beatrice = realm.createObject(Frog.class);    beatrice.setName("Beatrice");    dictionary.putAll(Map.of("small frog", greg, "feathered frog", beatrice));});
var frog: Frog? = nullrealm.executeTransaction { r: Realm? ->    frog = realm.createObject(Frog::class.java)    frog?.name = "Jonathan Livingston Applesauce"}val mapChangeListener: MapChangeListener<String, Frog>        = MapChangeListener<String, Frog> { map, changes ->    for (insertion in changes.insertions) {        Log.v("EXAMPLE",                "Inserted key: $insertion, Inserted value: ${map[insertion]!!.name}")    }}frog?.nicknamesToFriends?.addChangeListener(mapChangeListener)realm.executeTransaction { r: Realm? ->        val dictionary = frog!!.nicknamesToFriends        val wirt = realm.createObject(Frog::class.java)    wirt.name = "Wirt"    dictionary["tall frog"] = wirt        val greg = realm.createObject(Frog::class.java)    greg.name = "Greg"    val beatrice = realm.createObject(Frog::class.java)    beatrice.name = "Beatrice"    dictionary.putAll(mapOf<String, Frog>(            Pair("small frog", greg),            Pair("feathered frog", beatrice)))}

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4