| Znav |
|---|
| next | Java Mapping for Constants |
|---|
| prev | Java Mapping for Sequences |
|---|
|
Here is the definition of our EmployeeMap once more:
| Wiki Markup |
|---|
{zcode:slice}
dictionary<long, Employee> EmployeeMap;
{zcode} |
As for sequences, the Java mapping does not create a separate named type for this definition. Instead, the dictionary is simply an instance of the generic type java.util.Map<K, V>, where K is the mapping of the key type and V is the mapping of the value type. In the example above, EmployeeMap is mapped to the Java type java.util.Map<Long, Employee>. The following code demonstrates how to allocate and use an instance of EmployeeMap:
| Wiki Markup |
|---|
{zcode:java}
java.util.Map<Long, Employee> em = new java.util.HashMap<Long, Employee>();
Employee e = new Employee();
e.number = 31;
e.firstName = "James";
e.lastName = "Gosling";
em.put(e.number, e);
{zcode} |
The type-safe nature of the mapping makes iterating over the dictionary quite convenient:
| Wiki Markup |
|---|
{zcode:java}
for (java.util.Map.Entry<Long, Employee> i : em.entrySet()) {
long num = i.getKey();
Employee emp = i.getValue();
System.out.println(emp.firstName + " was employee #" + num);
}
{zcode} |
Alternate mappings for dictionary types are also possible.
See Also
| Znav |
|---|
| next | Java Mapping for Constants |
|---|
| prev | Java Mapping for Sequences |
|---|
|