Maps are associative collections: key -> item.

The main operator over maps is [].

var map: Map[String, Int] = new ArrayMap[String, Int]
# ...
map["one"] = 1      # Associate 'one' to '1'
map["two"] = 2      # Associate 'two' to '2'
assert map["one"]             ==  1
assert map["two"]             ==  2

Instances of maps can be used with the for structure

for key, value in map do
    assert (key == "one" and value == 1) or (key == "two" and value == 2)
end

The keys and values in the map can also be manipulated directly with the keys and values methods.

assert map.keys.has("one")    ==  true
assert map.keys.has("tree")   ==  false
assert map.values.has(1)      ==  true
assert map.values.has(3)      ==  false

Introduced properties

abstract fun []=(key: K, value: V)

core :: Map :: []=

Set the value at key.
fun add_all(map: MapRead[K, V])

core :: Map :: add_all

Add each (key,value) of map into self.
abstract fun clear

core :: Map :: clear

Remove all items
abstract fun join(sep: String, couple_sep: String): String

core :: Map :: join

Concatenate couples of key value.
init new: Map[K, V]

core :: Map :: new

Get a HashMap[K, V] as default implementation
fun recover_with(map: MapRead[K, V])

core :: Map :: recover_with

Alias for add_all

Redefined properties

redef type SELF: Map[K, V]

core $ Map :: SELF

Type of this instance, automatically specialized in every class
redef fun accept_json_serializer(v: JsonSerializer)

json :: serialization_write $ Map :: accept_json_serializer

Refinable service to customize the serialization of this class to JSON
redef fun accept_msgpack_serializer(v: MsgPackSerializer)

msgpack :: serialization_write $ Map :: accept_msgpack_serializer

Hook to customize the serialization of this class to MessagePack
redef init from_deserializer(v: Deserializer)

json :: serialization_read $ Map :: from_deserializer

Create an instance of this class from the deserializer
redef init from_deserializer(v: Deserializer)

msgpack :: serialization_read $ Map :: from_deserializer

Create an instance of this class from the deserializer
redef fun join(sep: String, couple_sep: String): String

core :: flat $ Map :: join

Concatenate couples of key value.
redef abstract fun keys: RemovableCollection[K]

core $ Map :: keys

Return the point of view of self on the keys only.
redef fun msgpack_extra_array_items: Int

msgpack :: serialization_write $ Map :: msgpack_extra_array_items

Hook to request a larger than usual metadata array
redef abstract fun values: RemovableCollection[V]

core $ Map :: values

Return the point of view of self on the values only.

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
abstract fun [](key: nullable Object): V

core :: MapRead :: []

Get the item at key
abstract fun []=(key: K, value: V)

core :: Map :: []=

Set the value at key.
protected fun accept_json_serializer(v: JsonSerializer)

serialization :: Serializable :: accept_json_serializer

Refinable service to customize the serialization of this class to JSON
protected fun accept_msgpack_attribute_counter(v: AttributeCounter)

serialization :: Serializable :: accept_msgpack_attribute_counter

Hook to customize the behavior of the AttributeCounter
protected fun accept_msgpack_serializer(v: MsgPackSerializer)

serialization :: Serializable :: accept_msgpack_serializer

Hook to customize the serialization of this class to MessagePack
fun add_all(map: MapRead[K, V])

core :: Map :: add_all

Add each (key,value) of map into self.
protected fun add_to_bundle(bundle: NativeBundle, key: JavaString)

serialization :: Serializable :: add_to_bundle

Called by []= to dynamically choose the appropriate method according
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
abstract fun clear

core :: Map :: clear

Remove all items
fun core_serialize_to(serializer: Serializer)

serialization :: Serializable :: core_serialize_to

Actual serialization of self to serializer
fun filter_keys(keys: Collection[nullable Object]): Array[K]

core :: MapRead :: filter_keys

Return all elements of keys that have a value.
init from_deserializer(deserializer: Deserializer)

serialization :: Serializable :: from_deserializer

Create an instance of this class from the deserializer
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun get_or_default(key: nullable Object, default: V): V

core :: MapRead :: get_or_default

Get the item at key or return default if not in map
fun get_or_null(key: nullable Object): nullable V

core :: MapRead :: get_or_null

Get the item at key or null if key is not in the map.
fun has_key(key: nullable Object): Bool

core :: MapRead :: has_key

Is there an item associated with key?
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
abstract fun is_empty: Bool

core :: MapRead :: is_empty

Is there no item in the collection?
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
abstract fun iterator: MapIterator[K, V]

core :: MapRead :: iterator

Get a new iterator on the map.
abstract fun join(sep: String, couple_sep: String): String

core :: Map :: join

Concatenate couples of key value.
abstract fun keys: Collection[K]

core :: MapRead :: keys

Return the point of view of self on the keys only.
fun keys_sorted_by_values(comparator: Comparator): Array[K]

core :: MapRead :: keys_sorted_by_values

Return an array of all keys sorted with their values using comparator.
abstract fun length: Int

core :: MapRead :: length

Number of items in the collection.
fun lookup_all_values(pe: POSetElement[K]): Set[V]

core :: MapRead :: lookup_all_values

Search all the values in pe.greaters.
fun lookup_values(pe: POSetElement[K]): Set[V]

core :: MapRead :: lookup_values

Combine the values in pe.greaters from the most smaller elements that have a value.
protected fun msgpack_extra_array_items: Int

serialization :: Serializable :: msgpack_extra_array_items

Hook to request a larger than usual metadata array
init new: Map[K, V]

core :: Map :: new

Get a HashMap[K, V] as default implementation
fun not_empty: Bool

core :: MapRead :: not_empty

Alias for not is_empty.
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
protected fun provide_default_value(key: nullable Object): V

core :: MapRead :: provide_default_value

Called by the underling implementation of [] to provide a default value when a key has no value
fun recover_with(map: MapRead[K, V])

core :: Map :: recover_with

Alias for add_all
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun serialize_msgpack(plain: nullable Bool): Bytes

serialization :: Serializable :: serialize_msgpack

Serialize self to MessagePack bytes
fun serialize_to(serializer: Serializer)

serialization :: Serializable :: serialize_to

Serialize self to serializer
fun serialize_to_json(plain: nullable Bool, pretty: nullable Bool): String

serialization :: Serializable :: serialize_to_json

Serialize self to JSON
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
fun to_json: String

serialization :: Serializable :: to_json

Serialize self to plain JSON
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_map_comparator(comparator: Comparator): MapComparator[K, V]

core :: MapRead :: to_map_comparator

A comparator that compares things with their values in self.
fun to_pretty_json: String

serialization :: Serializable :: to_pretty_json

Serialize self to plain pretty JSON
fun to_s: String

core :: Object :: to_s

User readable representation of self.
abstract fun values: Collection[V]

core :: MapRead :: values

Return the point of view of self on the values only.
fun values_sorted_by_key(comparator: Comparator): Array[V]

core :: MapRead :: values_sorted_by_key

Return an array of all values sorted with their keys using comparator.
package_diagram core::Map Map serialization::Serializable Serializable core::Map->serialization::Serializable core::MapRead MapRead core::Map->core::MapRead core::Object Object serialization::Serializable->core::Object core::MapRead->core::Object ...core::Object ... ...core::Object->core::Object core::CoupleMap CoupleMap core::CoupleMap->core::Map core::HashMap HashMap core::HashMap->core::Map trees::TreeMap TreeMap trees::TreeMap->core::Map counter::Counter Counter counter::Counter->core::Map trees::Trie Trie trees::Trie->core::Map core::ArrayMap ArrayMap core::ArrayMap->core::CoupleMap core::ArrayMap... ... core::ArrayMap...->core::ArrayMap core::HashMap... ... core::HashMap...->core::HashMap trees::BinTreeMap BinTreeMap trees::BinTreeMap->trees::TreeMap trees::BinTreeMap... ... trees::BinTreeMap...->trees::BinTreeMap

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

interface MapRead[K: nullable Object, V: nullable Object]

core :: MapRead

MapRead are abstract associative collections: key -> item.
interface Serializable

serialization :: Serializable

Instances of this class can be passed to Serializer::serialize

Children

class Counter[E: nullable Object]

counter :: Counter

A counter counts occurrences of things
interface CoupleMap[K: nullable Object, V: nullable Object]

core :: CoupleMap

Associative arrays that internally uses couples to represent each (key, value) pairs.
class HashMap[K: nullable Object, V: nullable Object]

core :: HashMap

A Map implemented with a hash table.
abstract class TreeMap[K: Comparable, E: nullable Object]

trees :: TreeMap

Abstract tree map structure
class Trie[E: nullable Object]

trees :: Trie

Trie data structure for prefix searches

Descendants

class ArrayMap[K: nullable Object, E: nullable Object]

core :: ArrayMap

Associative arrays implemented with an array of (key, value) pairs.
class AttributeMap

dot :: AttributeMap

Map of graph/node/edge attribute that can be rendered to dot.
class AttributeMap

gamnit :: AttributeMap

Map to organize Attribute instances by their name
class BinTreeMap[K: Comparable, E: nullable Object]

trees :: BinTreeMap

Binary Tree Map
class DefaultMap[K: nullable Object, V: nullable Object]

more_collections :: DefaultMap

A map with a default value.
class IniFile

ini :: IniFile

Read and write INI configuration files
class IniSection

ini :: IniSection

A section in a IniFile
class JsonObject

json :: JsonObject

A JSON Object.
class MongoGroup

mongodb :: MongoGroup

Mongo pipeline group stage
class MongoMatch

mongodb :: MongoMatch

A basic match query
class MultiHashMap[K: nullable Object, V: nullable Object]

more_collections :: MultiHashMap

Simple way to store an HashMap[K, Array[V]]
class PRMap[V: nullable Object]

graph :: PRMap

Map each Vertice of a Digraph to it's PageRank.
class PerfMap

performance_analysis :: PerfMap

Collection of statistics on many events
class RBTreeMap[K: Comparable, E: nullable Object]

trees :: RBTreeMap

Red-Black Tree Map
abstract class ShaderVariableMap[A: ShaderVariable]

gamnit :: ShaderVariableMap

Map to organize ShaderVariable instances by their name
class StrictHashMap[K: nullable Object, V: nullable Object]

serialization :: StrictHashMap

Maps instances to a value, uses is_same_serialized and serialization_hash.
class UniformMap

gamnit :: UniformMap

Map to organize Uniform instances by their name
class Vector

vsm :: Vector

A n-dimensions vector

Class definitions

core $ Map
# Maps are associative collections: `key` -> `item`.
#
# The main operator over maps is [].
#
#     var map: Map[String, Int] = new ArrayMap[String, Int]
#     # ...
#     map["one"] = 1      # Associate 'one' to '1'
#     map["two"] = 2      # Associate 'two' to '2'
#     assert map["one"]             ==  1
#     assert map["two"]             ==  2
#
# Instances of maps can be used with the for structure
#
#     for key, value in map do
#         assert (key == "one" and value == 1) or (key == "two" and value == 2)
#     end
#
# The keys and values in the map can also be manipulated directly with the `keys` and `values` methods.
#
#     assert map.keys.has("one")    ==  true
#     assert map.keys.has("tree")   ==  false
#     assert map.values.has(1)      ==  true
#     assert map.values.has(3)      ==  false
#
interface Map[K, V]
	super MapRead[K, V]

	# Set the `value` at `key`.
	#
	# Values can then get retrieved with `[]`.
	#
	#     var x = new HashMap[String, Int]
	#     x["four"] = 4
	#     assert x["four"]   == 4
	#
	# If the key was associated with a value, this old value is discarded
	# and replaced with the new one.
	#
	#     x["four"] = 40
	#     assert x["four"]         == 40
	#     assert x.values.has(4)   == false
	#
	fun []=(key: K, value: V) is abstract

	# Add each (key,value) of `map` into `self`.
	# If a same key exists in `map` and `self`, then the value in self is discarded.
	#
	#     var x = new HashMap[String, Int]
	#     x["four"] = 4
	#     x["five"] = 5
	#     var y = new HashMap[String, Int]
	#     y["four"] = 40
	#     y["nine"] = 90
	#     x.add_all y
	#     assert x["four"]  == 40
	#     assert x["five"]  == 5
	#     assert x["nine"]  == 90
	fun add_all(map: MapRead[K, V])
	do
		var i = map.iterator
		while i.is_ok do
			self[i.key] = i.item
			i.next
		end
	end

	# Alias for `add_all`
	fun recover_with(map: MapRead[K, V]) is deprecated do add_all(map)

	# Remove all items
	#
	#     var x = new HashMap[String, Int]
	#     x["four"] = 4
	#     x.clear
	#     assert x.keys.has("four") == false
	#
	# ENSURE `is_empty`
	fun clear is abstract

	redef fun values: RemovableCollection[V] is abstract

	redef fun keys: RemovableCollection[K] is abstract
end
lib/core/collection/abstract_collection.nit:684,1--766,3

core :: hash_collection $ Map
redef class Map[K, V]
	# Get a `HashMap[K, V]` as default implementation
	new do return new HashMap[K, V]
end
lib/core/collection/hash_collection.nit:18,1--21,3

core :: abstract_text $ Map
redef class Map[K,V]
	# Concatenate couples of key value.
	# Key and value are separated by `couple_sep`.
	# Couples are separated by `sep`.
	#
	# ~~~
	# var m = new HashMap[Int, String]
	# m[1] = "one"
	# m[10] = "ten"
	# assert m.join("; ", "=") == "1=one; 10=ten"
	# ~~~
	fun join(sep, couple_sep: String): String is abstract
end
lib/core/text/abstract_text.nit:2414,1--2426,3

core :: flat $ Map
redef class Map[K,V]
	redef fun join(sep, couple_sep)
	do
		if is_empty then return ""

		var s = new Buffer # Result

		# Concat first item
		var i = iterator
		var k = i.key
		var e = i.item
		s.append("{k or else "<null>"}{couple_sep}{e or else "<null>"}")

		# Concat other _items
		i.next
		while i.is_ok do
			s.append(sep)
			k = i.key
			e = i.item
			s.append("{k or else "<null>"}{couple_sep}{e or else "<null>"}")
			i.next
		end
		return s.to_s
	end
end
lib/core/text/flat.nit:1570,1--1594,3

serialization :: serialization_core $ Map
redef class Map[K, V] super Serializable end
lib/serialization/serialization_core.nit:266,1--44

serialization :: inspect $ Map
redef class Map[K, V]
	redef fun accept_inspect_serializer_core(v)
	do
		v.stream.write " \{"

		var first = true
		for key, val in self do
			if not first then
				v.stream.write ", "
			else first = false

			if not v.try_to_serialize(key) then
				assert key != null
				v.stream.write key.inspect
			end

			v.stream.write ":"

			if not v.try_to_serialize(val) then
				assert val != null
				v.stream.write val.inspect
			end
		end

		v.stream.write "\}"
	end
end
lib/serialization/inspect.nit:268,1--294,3

msgpack :: serialization_write $ Map
redef class Map[K, V]
	redef fun accept_msgpack_serializer(v)
	do
		if not v.plain_msgpack then
			# Add metadata and other attributes
			super
		end

		# Header
		v.stream.write_msgpack_map keys.length

		# Key / values, alternating
		for key, val in self do
			if not v.try_to_serialize(key) then
				assert val != null # null would have been serialized
				v.warn "element of type {val.class_name} is not serializable."
				v.stream.write_msgpack_null
			end

			if not v.try_to_serialize(val) then
				assert val != null # null would have been serialized
				v.warn "element of type {val.class_name} is not serializable."
				v.stream.write_msgpack_null
			end
		end
	end

	redef fun msgpack_extra_array_items do return 1
end
lib/msgpack/serialization_write.nit:321,1--349,3

json :: serialization_read $ Map
redef class Map[K, V]
	redef init from_deserializer(v)
	do
		super

		if v isa JsonDeserializer then
			v.notify_of_creation self
			init

			var keys_type_name = (new GetName[K]).to_s
			var values_type_name = (new GetName[V]).to_s

			var length = v.deserialize_attribute("__length")
			var keys = v.path.last.get_or_null("__keys")
			var values = v.path.last.get_or_null("__values")

			if keys == null and values == null then
				# Fallback to a plain object
				for key, value_src in v.path.last do

					var value = v.convert_object(value_src, values_type_name)

					if not key isa K then
						v.errors.add new AttributeTypeError(self, "keys", key, keys_type_name)
						continue
					end

					if not value isa V then
						v.errors.add new AttributeTypeError(self, "values", value, values_type_name)
						continue
					end

					self[key] = value
				end
				return
			end

			# Length is optional
			if length == null and keys isa SequenceRead[nullable Object] then length = keys.length

			# Consistency check
			if not length isa Int or length < 0 or
			   not keys isa SequenceRead[nullable Object] or
			   not values isa SequenceRead[nullable Object] or
			   keys.length != values.length or length != keys.length then

				# If there is nothing or length == 0, we consider that it is an empty Map.
				if (length != null and length != 0) or keys != null or values != null then
					v.errors.add new Error("Deserialization Error: invalid format in {self.class_name}")
				end
				return
			end

			# First, convert all keys to follow the order of the serialization
			var converted_keys = new Array[K]
			for i in length.times do
				var key = v.convert_object(keys[i], keys_type_name)

				if not key isa K then
					v.errors.add new AttributeTypeError(self, "keys", key, keys_type_name)
					continue
				end

				converted_keys.add key
			end

			# Then convert the values and build the map
			for i in length.times do
				var key = converted_keys[i]
				var value = v.convert_object(values[i], values_type_name)

				if not value isa V then
					v.errors.add new AttributeTypeError(self, "values", value, values_type_name)
					continue
				end

				if has_key(key) then
					v.errors.add new Error("Deserialization Error: duplicated key '{key or else "null"}' in {self.class_name}, previous value overwritten")
				end

				self[key] = value
			end
		end
	end
end
lib/json/serialization_read.nit:419,1--503,3

msgpack :: serialization_read $ Map
redef class Map[K, V]
	redef init from_deserializer(v)
	do
		super

		if v isa MsgPackDeserializer then
			v.notify_of_creation self
			init

			var open_object = v.path_arrays.last
			var msgpack_items
			if open_object != null then
				# Metadata available
				msgpack_items = open_object.last
			else
				msgpack_items = v.path.last
			end

			if not msgpack_items isa Map[nullable Object, nullable Object] then
				v.errors.add new Error("Deserialization Error: no key/values in source of `{class_name}`")
				return
			end

			var keys_type_name = (new GetName[K]).to_s
			var values_type_name = (new GetName[V]).to_s

			for key_src, value_src in msgpack_items do
				var key = v.convert_object(key_src, keys_type_name)
				if not key isa K then
					v.errors.add new AttributeTypeError(self, "keys", key, keys_type_name)
					continue
				end

				var value = v.convert_object(value_src, values_type_name)
				if not value isa V then
					v.errors.add new AttributeTypeError(self, "values", value, values_type_name)
					continue
				end

				self[key] = value
			end
		end
	end
end
lib/msgpack/serialization_read.nit:367,1--410,3