Map CLASS

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

All Members


TypeNameSignature
constructorMappublic Map()
methodatpublic var at(var)
methodatpublic void at(var, var)
methodgetpublic var get(var)
methodgetEntriespublic var[] getEntries()
methodgetIteratorpublic IIterator getIterator()
methodgetKeyspublic var[] getKeys()
methodhasKeypublic bool hasKey(var)
methodinitByMappublic void initByMap(System.Util.Entry[])
methodputpublic void put(var, var)
methodremovepublic var remove(var)
methodsizepublic int size()

Constructors


public Map()

Create a new map instance.


Methods


public var at(var key)

Get value by the specified key.

Parameters

  • key The key to the map.

Returns

  • Null if not found. Thus undistinguishable from null value.

Throws


public void at(var key, var value)

Set value by the specified key.

Parameters

  • key The key to the map.
  • value The value to set.

Throws


public var get(var key)

Get the value from the map by the specified key.

Parameters

  • key The key to the map.

Returns

  • If the key existed, the value associated with this key; otherwise, null. Note if the value is null this method cannot tell if a value of null has been retrieved or the key didn't exist. If such information is required, call hasKey beforehand.

Throws


public var[] getEntries()

Get all the entries stored in this map.

Returns

  • All the entries stored in this map.

public IIterator getIterator()

Get an iterator of this map.

Returns


public var[] getKeys()

Get all the keys stored in this map.

Returns

  • All the keys stored in this map.

public bool hasKey(var key)

Check if the specified key exists in the map, without getting the value associated with it.

Parameters

  • key The key to the map.

Returns

  • true if the key exists; false otherwise.

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

  • entries The key-value pairs to be added to this map.

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

  • key The key to the map.
  • value The value to store under this key. Can be null.

Throws


public var remove(var key)

Remove the specified key from the map.

Parameters

  • key The key to the map.

Returns

  • If the key existed, the value associated with this key; otherwise, null. Note if the value is null this method cannot tell if a value of null has been removed or the key didn't exist. If such information is required, call hasKey beforehand.

public int size()

The size of map.

Returns

  • Always non-negative. 0 if empty.