C# to Java - Dictionaries?

后端 未结 5 1211
伪装坚强ぢ
伪装坚强ぢ 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:30

    Java7 almost introduced "collection literals" that would allow syntax like that. They'll probably try to shove it in Java8. I have no idea what is wrong with these people.

    This can be easily achieved by some kind of wrapper API

    Map map = Maps.empty()
        .put("cat", 2).put("dog",1)....; 
    

    Not too bad. I would prefer something like

    map("cat", "dog", ... )
    .to(  1,     2,   ... );
    

    This kind of thing must have been implemented by various people, unfortunately the standard API doesn't inculde such things.

提交回复
热议问题