Criar um mapa em Java

Eu gostaria de criar um map que contém entradas que consistem em (int, Point2D)

Como posso fazer isto em Java?

Tentei o seguinte sem sucesso.

HashMap hm = new HashMap();

hm.put(1, new Point2D.Double(50, 50));
 45
Author: Kevin Meredith, 2013-02-07

4 answers

Map <Integer, Point2D.Double> hm = new HashMap<Integer, Point2D>();
hm.put(1, new Point2D.Double(50, 50));
 77
Author: hd1, 2013-02-07 04:26:55

Há ainda uma maneira melhor de criar um mapa junto com a inicialização:

Map<String, String> rightHereMap = new HashMap<String, String>()
{
    {
        put("key1", "value1");
        put("key2", "value2");
    }
};

Para mais opções, veja aqui Como posso inicializar um mapa estático?

 7
Author: webdizz, 2017-06-04 08:30:33
Map<Integer, Point2D> hm = new HashMap<Integer, Point2D>();
 6
Author: Achrome, 2013-02-07 04:36:36

Java 9

public static void main(String[] args) {
    Map<Integer,String> map = Map.ofEntries(entry(1,"A"), entry(2,"B"), entry(3,"C"));
}
 1
Author: Durgpal Singh, 2018-04-28 06:59:50