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


7374
7375
# File 'Monkeybrains.rb', line 7374

def get(key)
end

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

Determine whether a key exists within a Dictionary.

Parameters:

Returns:

Since:

  • 1.0.0


7382
7383
# File 'Monkeybrains.rb', line 7382

def hasKey(key)
end

isEmptyToybox::Lang::Boolean

Determine whether a Dictionary is empty.

Returns:

Since:

  • 1.0.0


7401
7402
# File 'Monkeybrains.rb', line 7401

def isEmpty()
end

keysToybox::Lang::Array

Retrieve the keys in the Dictionary.

Returns:

Since:

  • 1.0.0


7407
7408
# File 'Monkeybrains.rb', line 7407

def keys()
end

put(key, value) ⇒ Object

Place a value in the Dictionary with a given key.

Parameters:

Since:

  • 1.0.0


7366
7367
# File 'Monkeybrains.rb', line 7366

def put(key, value)
end

remove(key) ⇒ Object

Delete an item from a Dictionary.

Parameters:

Since:

  • 1.0.0


7388
7389
# File 'Monkeybrains.rb', line 7388

def remove(key)
end

sizeToybox::Lang::Number

Retrieve the number of elements in a Dictionary.

Returns:

Since:

  • 1.0.0


7394
7395
# File 'Monkeybrains.rb', line 7394

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


7436
7437
# File 'Monkeybrains.rb', line 7436

def toString()
end

valuesToybox::Lang::Array

Retrieve the values in the Dictionary.

Returns:

Since:

  • 1.0.0


7413
7414
# File 'Monkeybrains.rb', line 7413

def values()
end