A hash map to store data based on the calculated hash value, allowing fast element-targeting operation at O(1) cost.
An object stored in map will provide its hash through Object.hashCode() method. A primitive value will convert is numeric value into an integer to be used as hash. For example, byte value simply promotes the value to integer, and a float value get floored to an integer.
The hashcode is only a means to locate the slot in the map, but doesn't have to be unique. The uniqueness is determined by Object.equals() method. When putting a value into a map, if the slot suggested by the hashcode is already occupied, Object.equals()
is called between the new object and existing one. If the method returns true, the new object is not added; otherwise, the object will be added anyway. If there is more than one object using that slot, each of them will be compared against before the new object can be cleared of its uniqueness and added. The multiple objects stored on the same map slot are internally organized by a linked list. In best practice, try to make the hashcode as unique as possible to improve the performance, and always pay attention to equals() to ensure its proper functioning in determining the logical equality between two objects.
Julian supports map operation at language level. Use indexer syntax to achieve easy access: map[key] = value1;
var value2 = map[key];
Map is iterable. Each value returned during iteration is an Entry. Example: for (var entry : map) {
Console.println(entry.key + "=" + entry.value);
}
The methods of this class are not thread safe.
Parent Class
Parent Interfaces
Type | Name | Signature |
---|---|---|
constructor | Map | public Map() |
method | at | public var at(var) |
method | at | public void at(var, var) |
method | get | public var get(var) |
method | getEntries | public var[] getEntries() |
method | getIterator | public IIterator getIterator() |
method | getKeys | public var[] getKeys() |
method | hasKey | public bool hasKey(var) |
method | initByMap | public void initByMap(System.Util.Entry[]) |
method | put | public void put(var, var) |
method | remove | public var remove(var) |
method | size | public int size() |
public Map()
Create a new map instance.
public var at(var key)
Get value by the specified key.
Parameters
Returns
Throws
public void at(var key, var value)
Set value by the specified key.
Parameters
Throws
public var get(var key)
Get the value from the map by the specified key.
Parameters
Returns
Throws
public var[] getEntries()
Get all the entries stored in this map.
Returns
public IIterator getIterator()
Get an iterator of this map.
Returns
public var[] getKeys()
Get all the keys stored in this map.
Returns
public bool hasKey(var key)
Check if the specified key exists in the map, without getting the value associated with it.
Parameters
Returns
public void initByMap(Entry[] entries)
Initialize with an array of key-value pairs. Each pair adds one key to the map. If duplicated, the last one wins. Unlike put() where an ArgumentException will be thrown, null key is simply skipped.
Parameters
public void put(var key, var value)
Put a key/value pair into the map. The key's hashCode() is called to calculate the storage location, and its equals() method is used to determine the uniqueness.
Parameters
Throws
public var remove(var key)
Remove the specified key from the map.
Parameters
Returns
public int size()
The size of map.
Returns