【问题标题】:Add map elements to list with a loop使用循环将地图元素添加到列表中
【发布时间】:2013-09-09 21:30:22
【问题描述】:

我正在尝试使用从查询/findAllBy 中的对象创建的地图填充列表...我总是最终得到与循环中最后一张地图相同的地图列表。

我已设置断点并逐步执行该方法,发现 1) 从查询返回的数据是正确的,2) 当我逐步执行循环时,数据已正确插入到地图中,3)将地图插入列表时失败。我用来将元素插入列表的所有方法(.add、.push、

请帮忙。我不知道为什么会这样。希望这对外面的人来说是一件容易的事。

def shiftRecords = Shift.findAllByUserAndStartTimeBetween( userInstance, startDate, endDate )
ArrayList allShifts = new ArrayList()
LinkedHashMap thisShift = new LinkedHashMap()
def size = shiftRecords.size()

for ( int i = 0; i < size; i++ ){
    thisShift["id"] = shiftRecords[(i)].id
    thisShift["user"] = shiftRecords[(i)].user
    thisShift["startTime"] = shiftRecords[(i)].startTime
    thisShift["posCode"] = shiftRecords[(i)].posCode
    thisShift["deptCode"] = shiftRecords[(i)].deptCode
    thisShift["billingIDX"] = shiftRecords[(i)].billingIDX

    Position thisPos = Position.findByPositionCode( thisShift.posCode )
    thisShift["posTitle"] = thisPos.shortTitle
    thisShift["deptTitle"] = thisPos.departmentTitle

    allShifts.add( (i), thisShift )
}

我需要将 allShifts 的结果作为地图列表,其中包含从 Shift 查询结果中提取的选定数据。我试过使用 shiftRecords.each 和 eachWithIndex。在将 thisShift 映射插入到 allShifts 时,任何类型的循环都会出现问题。它不只是插入地图的一个实例,而是用当前的 thisShift 地图替换所有列表元素。

【问题讨论】:

    标签: list loops grails groovy


    【解决方案1】:
    def shiftRecords = Shift.findAllByUserAndStartTimeBetween( 
                                   userInstance, startDate, endDate )
    ArrayList allShifts = new ArrayList()
    def size = shiftRecords.size()
    
    for ( int i = 0; i < size; i++ ){
        LinkedHashMap thisShift = new LinkedHashMap()
        thisShift["id"] = shiftRecords[(i)].id
        thisShift["user"] = shiftRecords[(i)].user
        thisShift["startTime"] = shiftRecords[(i)].startTime
        thisShift["posCode"] = shiftRecords[(i)].posCode
        thisShift["deptCode"] = shiftRecords[(i)].deptCode
        thisShift["billingIDX"] = shiftRecords[(i)].billingIDX
    
        Position thisPos = Position.findByPositionCode( thisShift.posCode )
        thisShift["posTitle"] = thisPos.shortTitle
        thisShift["deptTitle"] = thisPos.departmentTitle
    
        allShifts << thisShift
    }
    

    每次迭代shiftRecords 时都需要创建一个新地图。虽然,上面的代码可以在 groovy 中过度简化如下:

    def shiftRecords = Shift.findAllByUserAndStartTimeBetween( 
                                   userInstance, startDate, endDate )
    def allShifts = []
    
    shiftRecords.each{
        def thisShift = [:]
        thisShift.id = it.id
        thisShift.user = it.user
        thisShift.startTime = it.startTime
        thisShift.posCode = it.posCode
        thisShift.deptCode = it.deptCode
        thisShift.billingIDX = it.billingIDX
    
        Position thisPos = Position.findByPositionCode( thisShift.posCode )
        thisShift.posTitle = thisPos.shortTitle
        thisShift.deptTitle = thisPos.departmentTitle
    
        allShifts << thisShift
    }
    

    【讨论】:

    • ahem 可能会更时髦(尽管我们必须注意不要“太时髦”)
    • @tim_yates 说得对,我喜欢小步,并指出方法中的错误。 OP:“我们可以进一步简化您的解决方案吗?”我:“是的,当然看看另一个答案;)”
    • 感谢您的回答。这太简单了,我很尴尬,我花了这么长时间把头发拉出来。我是 Groovy 和 Grails 的新手,有时事情过于复杂。
    • @CheddarMonkey 正如你所说,你是 Groovy 和 Grails 的新手。您需要知道接受的答案是如何工作的,而不仅仅是使用它。探索collect 的工作原理。我会花更多时间给你相同的答案(或类似的),而不是指出你的错误。但我更喜欢后者。 :)
    • @CheddarMonkey 从未要求接受我的回答。无论如何,蒂姆的回答将是您的最佳方法。 :)
    【解决方案2】:

    试试:

    def shiftRecords = Shift.findAllByUserAndStartTimeBetween( userInstance, startDate, endDate )
    def allShifts = shiftRecords.collect { it ->
        def pos = Position.findByPositionCode( it.posCode )
        [ id         : it.id,
          user       : it.user,
          startTime  : it.startTime,
          posCode    : it.posCode,
          deptCode   : it.deptCode,
          billingIDX : it.billingIDX,
          posTitle   : pos.shortTitle,
          deptTitle  : pos.departmentTitle ]
    }
    

    【讨论】:

    • 哈哈。也简化了我的解决方案。 :)
    • 感谢您为答案添加更多 GROOVE。我需要我能得到的所有建议。您和@dmahapatro 的出色回答。
    猜你喜欢
    • 1970-01-01
    • 2020-08-12
    • 2016-08-25
    • 2016-03-22
    • 1970-01-01
    • 1970-01-01
    • 2015-01-25
    • 2019-04-08
    • 2013-11-16
    相关资源
    最近更新 更多