groovy - collate maps in list of maps [closed]












-2















What is the simplest way to combine several maps in list elements into one map. For instance: list1 -> list2



def list1 = [[a:'apple'],[b:'orange'],[c:'pear'],[a:'watermelon'],[b:'banana'],[c:'grape'],[a:'lychee'],[b:'guava'],[c:'starfruit']]

def list2 = [[a:'apple', b:'orange', c:'pear'],[a:'watermelon', b:'banana', c:'grape'],[a:'lychee', b:'guava', c:'starfruit']]









share|improve this question















closed as too broad by doelleri, Owen Pauling, Szymon Stepniak, Pino, EdChum Nov 28 '18 at 12:34


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.



















  • What have you tried so far?

    – Szymon Stepniak
    Nov 28 '18 at 8:05
















-2















What is the simplest way to combine several maps in list elements into one map. For instance: list1 -> list2



def list1 = [[a:'apple'],[b:'orange'],[c:'pear'],[a:'watermelon'],[b:'banana'],[c:'grape'],[a:'lychee'],[b:'guava'],[c:'starfruit']]

def list2 = [[a:'apple', b:'orange', c:'pear'],[a:'watermelon', b:'banana', c:'grape'],[a:'lychee', b:'guava', c:'starfruit']]









share|improve this question















closed as too broad by doelleri, Owen Pauling, Szymon Stepniak, Pino, EdChum Nov 28 '18 at 12:34


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.



















  • What have you tried so far?

    – Szymon Stepniak
    Nov 28 '18 at 8:05














-2












-2








-2








What is the simplest way to combine several maps in list elements into one map. For instance: list1 -> list2



def list1 = [[a:'apple'],[b:'orange'],[c:'pear'],[a:'watermelon'],[b:'banana'],[c:'grape'],[a:'lychee'],[b:'guava'],[c:'starfruit']]

def list2 = [[a:'apple', b:'orange', c:'pear'],[a:'watermelon', b:'banana', c:'grape'],[a:'lychee', b:'guava', c:'starfruit']]









share|improve this question
















What is the simplest way to combine several maps in list elements into one map. For instance: list1 -> list2



def list1 = [[a:'apple'],[b:'orange'],[c:'pear'],[a:'watermelon'],[b:'banana'],[c:'grape'],[a:'lychee'],[b:'guava'],[c:'starfruit']]

def list2 = [[a:'apple', b:'orange', c:'pear'],[a:'watermelon', b:'banana', c:'grape'],[a:'lychee', b:'guava', c:'starfruit']]






groovy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '18 at 3:45







chunwai

















asked Nov 28 '18 at 7:30









chunwaichunwai

156




156




closed as too broad by doelleri, Owen Pauling, Szymon Stepniak, Pino, EdChum Nov 28 '18 at 12:34


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









closed as too broad by doelleri, Owen Pauling, Szymon Stepniak, Pino, EdChum Nov 28 '18 at 12:34


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.















  • What have you tried so far?

    – Szymon Stepniak
    Nov 28 '18 at 8:05



















  • What have you tried so far?

    – Szymon Stepniak
    Nov 28 '18 at 8:05

















What have you tried so far?

– Szymon Stepniak
Nov 28 '18 at 8:05





What have you tried so far?

– Szymon Stepniak
Nov 28 '18 at 8:05












1 Answer
1






active

oldest

votes


















1














Try this:



list1.groupBy{it.values()[0].intdiv(10)}.collect{it.value.collectEntries()}





share|improve this answer
























  • Hi Evgeny, this indeed give the result, but if those values in the map are string instead of int, is there any other workaround?

    – chunwai
    Nov 28 '18 at 10:30











  • change it.values()[0].intdiv(10) to it.values()[0].toInteger().intdiv(10)

    – Evgeny Smirnov
    Nov 28 '18 at 11:37











  • I mean some random non-numeric strings or numbers that doesn't follow the 1, 11, 21 pattern

    – chunwai
    Nov 28 '18 at 11:43











  • Then you must create other grouping criteria depending on what you want)

    – Evgeny Smirnov
    Nov 28 '18 at 15:17











  • Thanks Evgeny, I've figured it out. It turns out to be very simple. My solution: list1.collate(3).collect { it.collectEntries() }

    – chunwai
    Nov 29 '18 at 0:10


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Try this:



list1.groupBy{it.values()[0].intdiv(10)}.collect{it.value.collectEntries()}





share|improve this answer
























  • Hi Evgeny, this indeed give the result, but if those values in the map are string instead of int, is there any other workaround?

    – chunwai
    Nov 28 '18 at 10:30











  • change it.values()[0].intdiv(10) to it.values()[0].toInteger().intdiv(10)

    – Evgeny Smirnov
    Nov 28 '18 at 11:37











  • I mean some random non-numeric strings or numbers that doesn't follow the 1, 11, 21 pattern

    – chunwai
    Nov 28 '18 at 11:43











  • Then you must create other grouping criteria depending on what you want)

    – Evgeny Smirnov
    Nov 28 '18 at 15:17











  • Thanks Evgeny, I've figured it out. It turns out to be very simple. My solution: list1.collate(3).collect { it.collectEntries() }

    – chunwai
    Nov 29 '18 at 0:10
















1














Try this:



list1.groupBy{it.values()[0].intdiv(10)}.collect{it.value.collectEntries()}





share|improve this answer
























  • Hi Evgeny, this indeed give the result, but if those values in the map are string instead of int, is there any other workaround?

    – chunwai
    Nov 28 '18 at 10:30











  • change it.values()[0].intdiv(10) to it.values()[0].toInteger().intdiv(10)

    – Evgeny Smirnov
    Nov 28 '18 at 11:37











  • I mean some random non-numeric strings or numbers that doesn't follow the 1, 11, 21 pattern

    – chunwai
    Nov 28 '18 at 11:43











  • Then you must create other grouping criteria depending on what you want)

    – Evgeny Smirnov
    Nov 28 '18 at 15:17











  • Thanks Evgeny, I've figured it out. It turns out to be very simple. My solution: list1.collate(3).collect { it.collectEntries() }

    – chunwai
    Nov 29 '18 at 0:10














1












1








1







Try this:



list1.groupBy{it.values()[0].intdiv(10)}.collect{it.value.collectEntries()}





share|improve this answer













Try this:



list1.groupBy{it.values()[0].intdiv(10)}.collect{it.value.collectEntries()}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 28 '18 at 9:19









Evgeny SmirnovEvgeny Smirnov

2,0811618




2,0811618













  • Hi Evgeny, this indeed give the result, but if those values in the map are string instead of int, is there any other workaround?

    – chunwai
    Nov 28 '18 at 10:30











  • change it.values()[0].intdiv(10) to it.values()[0].toInteger().intdiv(10)

    – Evgeny Smirnov
    Nov 28 '18 at 11:37











  • I mean some random non-numeric strings or numbers that doesn't follow the 1, 11, 21 pattern

    – chunwai
    Nov 28 '18 at 11:43











  • Then you must create other grouping criteria depending on what you want)

    – Evgeny Smirnov
    Nov 28 '18 at 15:17











  • Thanks Evgeny, I've figured it out. It turns out to be very simple. My solution: list1.collate(3).collect { it.collectEntries() }

    – chunwai
    Nov 29 '18 at 0:10



















  • Hi Evgeny, this indeed give the result, but if those values in the map are string instead of int, is there any other workaround?

    – chunwai
    Nov 28 '18 at 10:30











  • change it.values()[0].intdiv(10) to it.values()[0].toInteger().intdiv(10)

    – Evgeny Smirnov
    Nov 28 '18 at 11:37











  • I mean some random non-numeric strings or numbers that doesn't follow the 1, 11, 21 pattern

    – chunwai
    Nov 28 '18 at 11:43











  • Then you must create other grouping criteria depending on what you want)

    – Evgeny Smirnov
    Nov 28 '18 at 15:17











  • Thanks Evgeny, I've figured it out. It turns out to be very simple. My solution: list1.collate(3).collect { it.collectEntries() }

    – chunwai
    Nov 29 '18 at 0:10

















Hi Evgeny, this indeed give the result, but if those values in the map are string instead of int, is there any other workaround?

– chunwai
Nov 28 '18 at 10:30





Hi Evgeny, this indeed give the result, but if those values in the map are string instead of int, is there any other workaround?

– chunwai
Nov 28 '18 at 10:30













change it.values()[0].intdiv(10) to it.values()[0].toInteger().intdiv(10)

– Evgeny Smirnov
Nov 28 '18 at 11:37





change it.values()[0].intdiv(10) to it.values()[0].toInteger().intdiv(10)

– Evgeny Smirnov
Nov 28 '18 at 11:37













I mean some random non-numeric strings or numbers that doesn't follow the 1, 11, 21 pattern

– chunwai
Nov 28 '18 at 11:43





I mean some random non-numeric strings or numbers that doesn't follow the 1, 11, 21 pattern

– chunwai
Nov 28 '18 at 11:43













Then you must create other grouping criteria depending on what you want)

– Evgeny Smirnov
Nov 28 '18 at 15:17





Then you must create other grouping criteria depending on what you want)

– Evgeny Smirnov
Nov 28 '18 at 15:17













Thanks Evgeny, I've figured it out. It turns out to be very simple. My solution: list1.collate(3).collect { it.collectEntries() }

– chunwai
Nov 29 '18 at 0:10





Thanks Evgeny, I've figured it out. It turns out to be very simple. My solution: list1.collate(3).collect { it.collectEntries() }

– chunwai
Nov 29 '18 at 0:10





Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)