Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- Ada
- C
- Caml
- Clojure
- C++
- C++
- C#
- C#
- D
- Dart
- Elixir
- Elixir
- Erlang
- Go
- Groovy
- Groovy
- Haskell
- JS
- JS
- JS
- Java
- Java
- Java
- Java
- Java
- Kotlin
- Kotlin
- Kotlin
- Lisp
- Lua
- Obj-C
- PHP
- Pascal
- Perl
- Python
- Python
- Ruby
- Rust
- Rust
- Scala
- Scheme
- Smalltalk
- VB
declare
package Maps is new Indefinite_Hashed_Maps (Key_Type => String,
Element_Type => Integer,
Hash => Ada.Strings.Hash,
Equivalent_Keys => "=");
use Maps;
X : Map := Empty_Map;
begin
X.Insert ("One", 1);
X.Insert ("Two", 2);
X.Insert ("Three", 3);
end;
ENTRY a = {"foo", "twenty"};
ENTRY b = {"bar", "three"};
if (hcreate (23)) {
hsearch(a, ENTER);
hsearch(b, ENTER);
}
This POSIX functions maintain a single global hashmap. The GNU C library provides hcreate_r
module StringMap = Map.Make(String)
let x =
StringMap.empty
|> StringMap.add "one" 1
|> StringMap.add "two" 2
|> StringMap.add "three" 3
(def x {"One" 1
"Two" 2
"Three" 3})
(get x "Two") will then return 2
var x = new Dictionary<string, int> {
["year"] = 2019,
["month"] = 12
};
int[string] x = ["one": 1, "two": 2];
Could also use 'auto' for x's type, as associative array literal syntax defines its type.
x = %{one: 1, two: 2}
x = %{"one" => 1, "two" => 2}
X = #{one => 1, "two" => 2.0, <<"three">> => [i, i, i]}.
const x = new Map([["one",1],["two",2]]);
The Map constructor can take an array of [key, value] pairs.
const x = {one: 1, two:2}
An object in JavaScript is essentially an associative array
const x = new Map();
x.set("one", 1);
x.set("two", 2);
From ES2015
Map<String, Integer> x = of("x", 1, "y", 2);
Immutable
import static java.util.Map.entry;
import static java.util.Map.ofEntries;
import java.util.Map;
import java.util.Map.Entry;
Entry<String, Integer> a = entry("x", 1),
b = entry("y", 2);
Map<String, Integer> x = ofEntries(a, b);
Map<String,Integer> x = new HashMap<>();
x.put("one", 1);
x.put("two", 2);
Map<String, Integer> x = new HashMap<>(of("x", 1, "y", 2));
Mutable
(let ((table (make-hash-table)))
(setf (gethash 'one table) 1)
(setf (gethash 'two table) 2))
x = {one = 1, two = 2}
NSDictionary *x=@{@"one":@1, @"two":@2};
Those pesky @'s are needed in ObjC to create objects (instead of the plain-C stuff, which ObjC supports fully with unchanged plain-C syntax)
$x = ['one' => 1, 'two' => 2];
type TMap = specialize TFPGMap<String, Integer>;
var x: TMap;
begin
x := TMap.Create;
x['one'] := 1;
x['two'] := 2;
end.
my %x = (
name => 'Roboticus',
'foo bar' => 'joe'
);
The '=>' operator autoquotes the word on its left if it begins with a letter or underscore and is composed only of letters, digits and underscores. Otherwise quotes are required.
x = {"one" : 1, "two" : 2}
x = dict(a=1, b=2, c=3)
let mut x = BTreeMap::new();
x.insert("one", 1);
x.insert("two", 2);
Something different than a BTreeMap might be used, depending on the usage.
The function new of the type BTreeMap returns a usable map.
The map is stored mutable in the variable x.
Maps in Rust are generic and type safe at compile time.
The function new of the type BTreeMap returns a usable map.
The map is stored mutable in the variable x.
Maps in Rust are generic and type safe at compile time.
val x = Map("a" -> 1, "b" -> 2, "c" -> 3)
(define x '(
("one" 1)
("two" 2)
("three" 3)))
Warning : the built-in association lists are inefficient, so don't use them for large data sets.
x := Dictionary newFrom: {
#a -> 1.
#b -> Object new}.
Alternate: { #a -> 1. #b -> Object new} asDictionary