| Hashtable |
|
typedict |
| |
A
Hashtable
is an unordered heterogeneous collection of key/value pairs.
It is different from a
Dictionary
because any two objects can be associated,
key/value pairs can be removed,
it is unordered and never has a size limit,
and standard dot, subscript, and pointer notation that work with
dictionaries can not be used to access the objects stored in a hashtable.
Yoix programs interact with hashtables by reading, writing, or executing
the following
Hashtable
fields:
| clone |
A read-only field that returns a new hashtable that contains the
same key/value pairs as this hashtable.
| | contains(Object key) |
A
Builtin
that returns an
int
that is non-zero when
key
is mapped to a value in this hashtable.
| | containsValue(Object value) |
A
Builtin
that returns an
int
that is non-zero when there is at least one key that is mapped to
value
in this hashtable.
| | find(Object value) |
A
Builtin
that returns one of the keys that is mapped to
value
in this hashtable, or
NULL
when value is not found.
Programs may need to use
containsValue
to determine whether
value
is actually defined, because
NULL
can be used as a key in a hashtable, so a
NULL
return from
find
does not always mean
value
is not defined.
| | findAll(Object value) |
A
Builtin
that returns an
Array
that contains all the keys that map to
value
in this hashtable, or
NULL
when
value
is not found.
| | get(Object key) |
A
Builtin
that returns the value associated with
key
in this hashtable, or
NULL
when
key
is not defined.
Programs may need to use
contains
to determine whether
key
is actually defined, because
NULL
can be stored in a hashtable, so a
NULL
return from
get
does not always mean
key
is not defined.
| | keys |
A read-only field that returns an
Array
containing every key object currently in this hashtable.
| | pairs |
An
Array,
arranged in pairs, that when read provides a snapshot, in a growable array,
that represents every object contained in this hashtable.
Objects at even indices are keys; objects at odd indices are the
corresponding values.
Writing clears the hashtable and then loads it with the key/value
pairs contained in the new array.
Assigning
NULL
to
pairs
is allowed and simply clears the hashtable.
| | put(Object key, Object value) |
A
Builtin
that associates
key
with
value
in this hashtable and returns the previous value associated with
key,
or
NULL
if
key
was not defined.
| | putAll(Object key1, Object value1, ...) |
A
Builtin
that defines one or more key/value pairs in this hashtable and
returns an
Array
that contains the previous key/value pairs for the keys that were
actually redefined by
putAll,
or
NULL
if there were no redefinitions.
| | remove(Object key) |
A
Builtin
that removes the entry associated with
key
from this hashtable and returns the previous value associated with
key,
or
NULL
if there was no prior association.
| | removeValue(Object value) |
A
Builtin
that removes all keys that are mapped to
value
from this hashtable and returns the number of objects removed.
| | size |
An
int
that represents the number of key/value pairs in this hashtable.
Assigning zero to
size
clears the hashtable;
assigning any other number to
size
accomplishes nothing and is silently ignored.
| | values |
A read-only field that returns an
Array
containing every value object currently in this hashtable.
|
Several permanent fields have not been documented and should not be
used in Yoix applications.
As already mentioned, hashtables are unordered, so programs should not
make any assumptions about order just because a field
(e.g.,
pairs)
returns an ordered object like an array.
| |
| Example: |
The program,
import yoix.*.*;
Hashtable colors;
JFrame f;
Array keys;
int i;
colors.putAll(unroll(Color));
colors.put("cornflowerblue", new Color {
double red = 0x64/255.0;
double green = 0x95/255.0;
double blue = 0xED/255.0;
});
colors.put("darkslateblue", new Color {
double red = 0x48/255.0;
double green = 0x3D/255.0;
double blue = 0x8B/255.0;
});
colors.put("teal", new Color {
double red = 0x00/255.0;
double green = 0x80/255.0;
double blue = 0x80/255.0;
});
colors.put("lightcoral", new Color {
double red = 0xF0/255.0;
double green = 0x80/255.0;
double blue = 0x80/255.0;
});
keys = colors.keys;
qsort(keys);
for(i = 0; i < keys@sizeof; i++) {
f.title = "Color is: " + keys[i];
f.background = colors.get(keys[i]);
if(!f.visible) f.visible = true;
sleep(3);
}
f.visible = false;
creates a hashtable and populates it first with the contents of the
yoix.awt.Color
dictionary, then it adds several additional colors to the hashtable.
After extracting the keys and sorting them, it creates a frame and
sequentially sets the frame title to the key value and the frame
background color to the corresponding color value stored for that
key in the hashtable.
| | |
| See Also: |
Array,
Dictionary,
String,
Vector
|
|
Yoix is a registered trademark of AT&T Inc.
|