如何将多个值推送到地图

时间:2019-04-17 18:42:36

标签: dictionary groovy

我必须将多个值推送到地图上,并在我尝试过的内容下方:

我有一个ename ='Test1',我必须检索他的所有详细信息。所以我确实查询了从empname获取详细信息,结果如下:

   String ename='Test1'
   Map<String, Map<String, Object>> empData = new HashMap<String, 
   HashMap<String, Object>>()

   ​def empDetails=[[job:'Dev',exp:2],[job:'QA',exp:4]]
   Map<String, Object> empColl = new HashMap<String, Object>() 
   empDetails.eachWithIndex{itr,index->  //looping through empDetails   
         empColl.put("job",itr.job);  
         empColl.put("exp",itr.exp); 
   }

  empData.put(ename,empColl);
  println empData;​  //I see only one value in the map

但是我希望期望的输出是:

  [Test1:[[job:'Dev',exp:2][job:QA, exp:4]]].

1 个答案:

答案 0 :(得分:2)

String ename='Test1'
def empDetails=[[job:'Dev',exp:2],[job:'QA',exp:4]]
def empData = ["$ename": empDetails]

返回

[Test1:[[job:Dev, exp:2], [job:QA, exp:4]]]