Class: Toybox.Lang.Dictionary

Inherits:
Toybox.Lang.Object show all

Overview

A Dictionary is a hash table or associative array used to map keys to values.

Both the keys and values can be any Object type, though they do not all need to be of the same type. Objects used as a keys should override the hashCode() method. Due to the nature of hash tables, the order of Dictionary elements are not guaranteed to match the insertion order.

See Also:

Example:

using Toybox.System;
var myDict = {
    "One" => 1,
    "Two" => 2,
    "Three" => 3
};
System.println(myDict);         // {Two=>2, One=>1,Three=>3}
var keys = myDict.keys();       // [Two, One, Three]
var values = myDict.values();   // [2, 1, 3]

myDict.put("Four", 4);
System.println(myDict);         // {Two=>2, One=>1, Three=>3, Four=>4}
numItems = myDict.size();       // 4

myDict = {}                     // Empty the dictionary
System.println(myDict.isEmpty()); // true

Since:

API Level 1.0.0

Instance Method Summary collapse

Instance Method Details

get(key as Lang.Object) as Lang.Object or Null

Retrieve a value from a Dictionary for a given key.

Parameters:

Returns:

  • Lang.Object

    The value for the specified key, or null if the key does not exist

Since:

API Level 1.0.0

hasKey(key as Lang.Object) as Lang.Boolean

Determine whether a key exists within a Dictionary.

Parameters:

Returns:

  • Lang.Boolean

    true if the key is in the Dictionary, otherwise false

Since:

API Level 1.0.0

isEmpty() as Lang.Boolean

Determine whether a Dictionary is empty.

Returns:

  • Lang.Boolean

    true if the Dictionary is empty, otherwise false

Since:

API Level 1.0.0

keys() as Lang.Array<Lang.Object>

Retrieve the keys in the Dictionary.

Returns:

  • Lang.Array

    An Array of keys in the Dictionary

Since:

API Level 1.0.0

put(key as Lang.Object, value as Lang.Object or Null) as Void

Place a value in the Dictionary with a given key.

Parameters:

  • key(Lang.Object)

    The key for the value being inserted into the Dictionary

  • value(Lang.Object)

    The value to insert into the Dictionary

Since:

API Level 1.0.0

remove(key as Lang.Object) as Void

Delete an item from a Dictionary.

Parameters:

  • key(Lang.Object)

    The key of the value to be removed

Since:

API Level 1.0.0

size() as Lang.Number

Retrieve the number of elements in a Dictionary.

Returns:

  • Lang.Number

    The number of elements in the Dictionary

Since:

API Level 1.0.0

toString() as Lang.String

Convert a Dictionary to a String.

Due to the nature of hash tables, the order of Dictionary elements are not guaranteed to match the insertion order when converting to a String.

Example:

using Toybox.System;
myDict = {
    "One" => 1,
    "Two" => 2,
    "Three" => 3
};
System.println(myDict.get("One"));        // prints 1

var myString = myDict.toString();
System.println(myString);                 // "{Two=>2, One=>1, Three=>}"
System.println(myString.get("One"));      // Symbol Not Found Error
System.println(myString.substring(0, 5)); // "{Two="

Returns:

  • Lang.String

    A String representation of the Dictionary

See Also:

Since:

API Level 1.0.1

values() as Lang.Array<Lang.Object or Null>

Retrieve the values in the Dictionary.

Returns:

  • Lang.Array

    An Array of values in the Dictionary

Since:

API Level 1.0.0


Generated Sep 23, 2024, 3:32:49 PM