Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Java
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 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.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 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...