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


6297
6298
# File 'Monkeybrains.rb', line 6297

def get(key)
end

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

Determine whether a key exists within a Dictionary.

Parameters:

Returns:

Since:

  • 1.0.0


6305
6306
# File 'Monkeybrains.rb', line 6305

def hasKey(key)
end

isEmptyToybox::Lang::Boolean

Determine whether a Dictionary is empty.

Returns:

Since:

  • 1.0.0


6324
6325
# File 'Monkeybrains.rb', line 6324

def isEmpty()
end

keysToybox::Lang::Array

Retrieve the keys in the Dictionary.

Returns:

Since:

  • 1.0.0


6330
6331
# File 'Monkeybrains.rb', line 6330

def keys()
end

put(key, value) ⇒ Object

Place a value in the Dictionary with a given key.

Parameters:

Since:

  • 1.0.0


6289
6290
# File 'Monkeybrains.rb', line 6289

def put(key, value)
end

remove(key) ⇒ Object

Delete an item from a Dictionary.

Parameters:

Since:

  • 1.0.0


6311
6312
# File 'Monkeybrains.rb', line 6311

def remove(key)
end

sizeToybox::Lang::Number

Retrieve the number of elements in a Dictionary.

Returns:

Since:

  • 1.0.0


6317
6318
# File 'Monkeybrains.rb', line 6317

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


6359
6360
# File 'Monkeybrains.rb', line 6359

def toString()
end

valuesToybox::Lang::Array

Retrieve the values in the Dictionary.

Returns:

Since:

  • 1.0.0


6336
6337
# File 'Monkeybrains.rb', line 6336

def values()
end