0

我有一张地图

[email:[hus@gmail.com, vin@gmail.com], jobTitle:[SE, SD], isLaptopRequired:[on, on], phone:[9908899876, 7765666543], name:[hus, Vin]]

为此我需要另一个地图

[hus:[hus@gmail.com,SE,99087665343],vin:[vin@gmail.com,SE,7765666543]] 

如何在 Groovy 中做到这一点?

4

2 回答 2

1

你可以这样做:

def map = [email:['hus@gmail.com', 'vin@gmail.com'], jobTitle:['SE', 'SD'], isLaptopRequired:['on', 'on'], phone:['9908899876', '7765666543'], name:['hus', 'Vin']]

def result = [:]

map.name.eachWithIndex { name, idx ->
  result << [ (name): map.values()*.getAt( idx ) - name ]
}

assert result == [hus:['hus@gmail.com', 'SE', 'on', '9908899876'], Vin:['vin@gmail.com', 'SD', 'on', '7765666543']]

或者,您也可以这样做:

def result = [map.name,map.findAll { it.key != 'name' }.values().toList().transpose()].transpose().collectEntries()

但这只是以牺牲可读性和资源使用为代价的更少代码;-)

于 2013-09-02T12:38:39.037 回答
0

我拥有的最直观的解决方案:

    def map = [email:['hus@gmail.com', 'vin@gmail.com'], jobTitle:['SE', 'SD'], isLaptopRequired:['on', 'on'], phone:['9908899876', '7765666543'], name:['hus', 'Vin']]


    def names = map.name
    def emails = map.email
    def jobTitles = map.jobTitle
    def isLaptopRequireds = map.isLaptopRequired //sorry for the variable name
    def phones = map.phone

    def result = [:]
    for(i in 0..names.size()-1) {
        result << [(names[i]): [emails[i], jobTitles[i], isLaptopRequireds[i], phones[i]]]
    }
    assert result == [hus:['hus@gmail.com', 'SE', 'on', '9908899876'], Vin:['vin@gmail.com', 'SD', 'on', '7765666543']]
}
于 2013-09-08T08:51:10.943 回答