Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

groovy - how to collate data in the form of maps in a list

I am currently working on processing the data as collate in order to generate chunk for processing large data.

The data is as follows in the form of a map in the list. [[userId:itemId] .... ]

I am trying to see many articles, but I have a question because it is harder to solve than I thought.

My code is as follows.

def list = []

def map = [userId:"YPNVNMLRHUQODZASXVC4KX69SE3X", itemId:24129345]
def map2 = [userId:"58F8QN9R695B07XLHGSFCSLGVM7H", itemId:99486445]
def map3 = [userId:"2L7R476MD5M3GA0DNDCH4UFVLG19", itemId:34217144]

list.add(map)
list.add(map2)
list.add(map3)

def testList = list.collate(2).collect { it ->
            it.collectEntries(){
                [it.userId, it.itemId]
            }
        }

The results of this code are as follows.

[[YPNVNMLRHUQODZASXVC4KX69SE3X:24129345, 58F8QN9R695B07XLHGSFCSLGVM7H:99486445], [2L7R476MD5M3GA0DNDCH4UFVLG19:34217144]]

This method works perfectly, but all I want is a list of keys, not just a list of values extracted.

So, is there a way to collate the data structure above with the key (userId, itemId) remaining?

To leave the key, I performed the following code.

def testList = list.collate(2).collect { it ->
    it.collectEntries()
}

However, the code cannot be collated the way I want it to be. The resulting values are shown below.

[[userId:58F8QN9R695B07XLHGSFCSLGVM7H, itemId:99486445], [userId:2L7R476MD5M3GA0DNDCH4UFVLG19, itemId:34217144]]

when collate (2) is done, the size of the list is 2, so a total of 2 lists should be made with 1 list of 2 elements merged and a list containing map3, but there is a value omitted as above.

How can we solve this?

question from:https://stackoverflow.com/questions/65661533/groovy-how-to-collate-data-in-the-form-of-maps-in-a-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...