How to directly initialize a HashMap (in a literal way)?

后端 未结 14 2379
野趣味
野趣味 2020-11-22 10:58

Is there some way of initializing a Java HashMap like this?:

Map test = 
    new HashMap{\"test\":\"test\",\"test\         


        
14条回答
  •  感情败类
    2020-11-22 11:16

    You can create a method to initialize the map like in this example below:

    Map initializeMap()
    {
      Map ret = new HashMap<>();
    
      //populate ret
      ...
    
      return ret;
    }
    
    //call
    Map map = initializeMap();
    

提交回复
热议问题