Class: Toybox::Lang::Dictionary

Inherits:
Object
  • 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.

Examples:

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

See Also:

Since:

  • 1.0.0

Instance Method Summary collapse

Methods inherited from Object

equals, hashCode, method, weak

Instance Method Details

get(key) ⇒ Toybox::Lang::Object

Retrieve a value from a Dictionary for a given key.

Parameters:

Returns:

Since:

  • 1.0.0


7377
7378
# File 'Monkeybrains.rb', line 7377

def get(key)
end

hasKey(key) ⇒ Toybox::Lang::Boolean

Determine whether a key exists within a Dictionary.

Parameters:

Returns:

Since:

  • 1.0.0


7385
7386
# File 'Monkeybrains.rb', line 7385

def hasKey(key)
end

isEmptyToybox::Lang::Boolean

Determine whether a Dictionary is empty.

Returns:

Since:

  • 1.0.0


7404
7405
# File 'Monkeybrains.rb', line 7404

def isEmpty()
end

keysToybox::Lang::Array

Retrieve the keys in the Dictionary.

Returns:

Since:

  • 1.0.0


7410
7411
# File 'Monkeybrains.rb', line 7410

def keys()
end

put(key, value) ⇒ Object

Place a value in the Dictionary with a given key.

Parameters:

Since:

  • 1.0.0


7369
7370
# File 'Monkeybrains.rb', line 7369

def put(key, value)
end

remove(key) ⇒ Object

Delete an item from a Dictionary.

Parameters:

Since:

  • 1.0.0


7391
7392
# File 'Monkeybrains.rb', line 7391

def remove(key)
end

sizeToybox::Lang::Number

Retrieve the number of elements in a Dictionary.

Returns:

Since:

  • 1.0.0


7397
7398
# File 'Monkeybrains.rb', line 7397

def size()
end

toStringToybox::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.

Examples:

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:

See Also:

Since:

  • 1.0.1


7439
7440
# File 'Monkeybrains.rb', line 7439

def toString()
end

valuesToybox::Lang::Array

Retrieve the values in the Dictionary.

Returns:

Since:

  • 1.0.0


7416
7417
# File 'Monkeybrains.rb', line 7416

def values()
end