Groovy groupBy或collect地图列表

Groovy groupBy或collect地图列表,groovy,Groovy,好的,我有这张地图列表: List data = [ [name: "John", age: 21, gender: "male", option1: "test", option2: "test2"], [name: "Doe", age: 23, gender: "male", option1: "", option2: "test2"], [name: "Kate", age: 25, gender: "female", option1: "test", optio

好的,我有这张地图列表:

List data = [
    [name: "John", age: 21, gender: "male", option1: "test", option2: "test2"],
    [name: "Doe", age: 23, gender: "male", option1: "", option2: "test2"],
    [name: "Kate", age: 25, gender: "female", option1: "test", option2: ""],
    [name: "Ann", age: 22, gender: "female", option1: "", option2: ""]
]
因此,我希望它们按如下方式分组:

Map<String, Object[]> data = new TreeMap<String, Object[]>()
    data.put("1", ["ID", "name", "age", "gender","option1", "option2"])
    data.put("2", [1, "John", 21, "male", "test", "test2"])
    data.put("3", [2, "Doe", 23, "male", "", "test2"])
    data.put("4", [3, "Kate", 25, "female", "test", ""])
    data.put("5", [4, "Ann", 21, "female", "", ""])
或者像这样的东西,因为我将把它插入excel文件。或者如果有人能推荐一种在groovy中实现这一点的好方法


谢谢

基本思想是创建一个新的树状图,因为你提到了它,但我认为LinkedHashMap应该足够了,把你的标题行放在那里,然后把输入列表中的所有条目都放在那里,并用移位的索引

还有一个技巧,如果你不需要,那么就用单引号而不是双引号

在这里,我花了一点时间,由于长时间没有开槽,我有点怀念这门伟大的语言:

List data = [     [name: "John", age: 21, gender: "male", option1: "test", option2: "test2"],     [name: "Doe", age: 23, gender: "male", option1: "", option2: "test2"],     [name: "Kate", age: 25, gender: "female", option1: "test", option2: ""],     [name: "Ann", age: 22, gender: "female", option1: "", option2: ""] ]

def exportData = [ 1 : [ 'ID', 'name', 'age', 'gender', 'option1', 'option2' ] ] as TreeMap

data.eachWithIndex { it, index  -> exportData.put(index+2, [index+1]+it.values())  }

println exportData​

试试看

因为电子表格是单元格的集合,每个单元格都有一列和一行,所以一个选项是将数据准备为单元格列表。下面是一个如何进行此操作的工作演示:

@Grab('net.sourceforge.jexcelapi:jxl:2.6.12')

import jxl.Workbook
import jxl.write.WritableCell
import jxl.write.Label
import jxl.write.Number as NumericCell

List data = [
    [name: "John", age: 21, gender: "male", option1: "test", option2: "test2"],
    [name: "Doe", age: 23, gender: "male", option1: "", option2: "test2"],
    [name: "Kate", age: 25, gender: "female", option1: "test", option2: ""],
    [name: "Ann", age: 22, gender: "female", option1: "", option2: ""]
]

new FileOutputStream(new File('test.xls')).withStream {stream ->
    def workbook = Workbook.createWorkbook(stream)
    def sheet = workbook.createSheet('sheet name goes here', 0)
    def cells = []

    // Create the header cells
    use(ExcelWritableCellCategory) {
        ['ID', 'name', 'age', 'gender', 'option1', 'option2'].eachWithIndex {name, column ->
            cells << name.toCell(column, 0)
        }
    }

    // Create the data cells
    data.eachWithIndex {map, index ->
        def row = index + 1

        use(ExcelWritableCellCategory) {
             cells << row.toCell(0, row)

            ['name', 'age', 'gender', 'option1', 'option2'].eachWithIndex {key, column ->
                cells << map[key].toCell(column + 1, row)
            }
        }
    }

    // Put the cells into the sheet
    cells.each { sheet.addCell it }    
    workbook.write()
    workbook.close()
}

class ExcelWritableCellCategory {
    static WritableCell toCell(Number number, int column, int row) {
        new NumericCell(column, row, number.toDouble())
    }

    static WritableCell toCell(String string, int column, int row) {
        new Label(column, row, string)
    }    
}

提示:我只是注意到一些冗余的钥匙列表;但这是一个简单的解决方案。

另一个选项是创建一个列表列表,前面加上标题名:

List data = [
    [name: "John", age: 21, gender: "male", option1: "test", option2: "test2"],
    [name: "Doe", age: 23, gender: "male", option1: "", option2: "test2"],
    [name: "Kate", age: 25, gender: "female", option1: "test", option2: ""],
    [name: "Ann", age: 22, gender: "female", option1: "", option2: ""]
]

List result = data[0].keySet().with { keys ->
    data.inject([keys.toList()]) { curr, next ->
        curr << keys.collect { next[it] }
    }
}

assert result == [
    ['name', 'age', 'gender', 'option1', 'option2'],
    ['John',  21,   'male',   'test',    'test2'  ],
    ['Doe',   23,   'male',   '',        'test2'  ],
    ['Kate',  25,   'female', 'test',    ''       ],
    ['Ann',   22,   'female', '',        ''       ]
]

非常感谢你!正是我需要的!