Groovy web console

subscribe to the feed Subscribe
to this

collectEntries vs groupBy

Published 6 months ago by Adrian
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import groovy.json.*
class Event {
    Integer Id
    String title
    Boolean exclusive

def events = [    new Event(id: 1, title: "Greach", exclusive: false),
    new Event(id: 2, title: "J-Spring", exclusive: true),
    new Event(id: 3, title: "GOTO", exclusive: false),
    new Event(id: 4, title: "Gr8Conf", exclusive: true)

def eventsCollected = events.collectEntries { Event event ->
    [, event]
println("collectEntries:\n" + new JsonBuilder( eventsCollected ).toPrettyString())

def eventsGrouped = events.groupBy { Event event ->
println("\ngroupBy:\n" + new JsonBuilder( eventsGrouped ).toPrettyString())