List Groovy合并两个列表?

List Groovy合并两个列表?,list,groovy,merge,List,Groovy,Merge,我有两份清单: listA: [[Name: mr good, note: good,rating:9], [Name: mr bad, note: bad, rating:5]] listB: [[Name: mr good, note: good,score:77], [Name: mr bad, note: bad, score:12]] 我想买这个 listC: [[Name: mr good, note: good,, rating:9, score:77], [Name:

我有两份清单:

listA: 
[[Name: mr good, note: good,rating:9], [Name: mr bad, note: bad, rating:5]] 

listB: 
[[Name: mr good, note: good,score:77], [Name: mr bad, note: bad, score:12]]
我想买这个

listC:
[[Name: mr good, note: good,, rating:9, score:77], [Name: mr bad, note: bad, rating:5,score:12]] 
我怎么做呢


谢谢。

收集listA中的所有元素,并在listB中找到与之等效的元素A。将其从listB中删除,并返回组合元素

如果我们说您的结构如上所述,我可能会:

def listC = listA.collect( { elementA ->
    elementB = listB.find { it.Name == elementA.Name }

    // Remove matched element from listB
    listB.remove(elementB)
    // if elementB == null, I use safe reference and elvis-operator
    // This map is the next element in the collect
    [
        Name: it.Name,
        note: "${it.note} ${elementB?.note :? ''}", // Perhaps combine the two notes?
        rating: it.rating?:0 + elementB?.rating ?: 0, // Perhaps add the ratings?
        score: it.score?:0 + elementB?.score ?: 0 // Perhaps add the scores?
    ] // Combine elementA + elementB anyway you like
}

// Take unmatched elements in listB and add them to listC
listC += listB 

这个问题的主题有些笼统,所以如果有人在这里寻找“如何在groovy中将两个列表合并到一个映射中?”的话,我将发布一个更简单问题的答案

导入groovy.util.logging.Slf4j
导入org.testng.annotations.Test
@试验
@Slf4j
类ExplorerEmergelistsOfMaps{
最终定义列表A=[[名称:“mr good”,注释:“good”,评级:9],[名称:“mr bad”,注释:“bad”,评级:5]]
最终定义列表B=[[名称:“mr good”,注释:“good”,分数:77],[名称:“mr bad”,注释:“bad”,分数:12]]
void tryGroupBy(){
def listIn=listA+listB
def group=listIn.groupBy{item->item.Name}
def answer=grouped.inject([],{candidate,item->candidate+=mergeMapkeys(item.value)})
log.debug(answer.dump())
}
私有def mergeMapkeys(列表映射){

def ret=maps.inject([:],{mergedMap,map->mergedMap你真的想在listC中使用两个逗号吗?列表中的元素什么时候被认为是相等的?例如,同名但不同的元素会发生什么情况?注意,listA和listB是映射,而不是ListNon。listA和listB是带有映射元素的列表。尽管我不想这么做这正是OP所做的,这帮了我的忙!谢谢!
def keys = "key1\nkey2\nkey3"
def values = "value1,value2,value3"
keys = keys.split("\n")
values = values.split(",")
def map = [:]
keys.eachWithIndex() {param,i -> map[keys[i]] = values[i] }
print map
import groovy.util.logging.Slf4j
import org.testng.annotations.Test

@Test
@Slf4j
class ExploreMergeListsOfMaps{
    final def listA = [[Name: 'mr good', note: 'good',rating:9], [Name: 'mr bad', note: 'bad',rating:5]]
    final def listB = [[Name: 'mr good', note: 'good',score:77], [Name: 'mr bad', note: 'bad', score:12]]

    void tryGroupBy() {
        def listIn = listA + listB
        def grouped = listIn.groupBy { item -> item.Name }
        def answer = grouped.inject([], { candidate, item -> candidate += mergeMapkeys( item.value )})
        log.debug(answer.dump())
    }

    private def mergeMapkeys( List maps ) {
        def ret =  maps.inject([:],{ mergedMap , map -> mergedMap << map })
        ret
    }
}