Logo

Programming-Idioms

  • Scheme
  • Java
  • Lisp
(let ((table (make-hash-table)))
  (setf (gethash 'one table) 1)
  (setf	(gethash 'two table) 2))
(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.
import static java.util.Map.of;
import java.util.HashMap;
import java.util.Map;
Map<String, Integer> x = new HashMap<>(of("x", 1, "y", 2));

Mutable
import java.util.Map;
import java.util.HashMap;
final Map<String, Integer> x = new HashMap<String, Integer>() {{
    put("one", 1);
    put("two", 2);
    put("three", 3);
}};
import java.util.Map;
import java.util.HashMap;
Map<String,Integer> x = new HashMap<>();
x.put("one", 1);
x.put("two", 2);
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);
import static java.util.Map.of;
import java.util.Map;
Map<String, Integer> x = of("x", 1, "y", 2);

Immutable
with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Strings.Hash;

use Ada.Containers;
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;

New implementation...