C# to Java - Dictionaries?

后端 未结 5 1212
伪装坚强ぢ
伪装坚强ぢ 2020-12-25 09:41

Is it possible in Java to make a Dictionary with the items already declared inside it? Just like the below C# code:

   Dictionary d = new          


        
5条回答
  •  无人及你
    2020-12-25 10:33

    Bite the bullet and type out the map name!

        Map map = new HashMap();
        map.put("cat", 2);
        map.put("dog", 1);
        map.put("llama", 0);
        map.put("iguana", -1);
    

    You could also do something like this, which might save some typing with a long list:

        Object[][] values = {
            {"cat", 2},
            {"dog", 1},
            {"llama", 0},
            {"iguana", -1}
        };
    
        for (Object[] o : values) {
            map.put((String) o[0], (Integer) o[1]);
        }
    

提交回复
热议问题