groovy - collate maps in list of maps [closed]
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
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.
add a comment |
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
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
add a comment |
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
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
groovy
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
Try this:
list1.groupBy{it.values()[0].intdiv(10)}.collect{it.value.collectEntries()}
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
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this:
list1.groupBy{it.values()[0].intdiv(10)}.collect{it.value.collectEntries()}
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
add a comment |
Try this:
list1.groupBy{it.values()[0].intdiv(10)}.collect{it.value.collectEntries()}
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
add a comment |
Try this:
list1.groupBy{it.values()[0].intdiv(10)}.collect{it.value.collectEntries()}
Try this:
list1.groupBy{it.values()[0].intdiv(10)}.collect{it.value.collectEntries()}
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
add a comment |
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
add a comment |
What have you tried so far?
– Szymon Stepniak
Nov 28 '18 at 8:05