Groovy web console

subscribe to the feed Subscribe
to this
site

collectEntries vs groupBy

Published 2 weeks 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.id, event]
}
println("collectEntries:\n" + new JsonBuilder( eventsCollected ).toPrettyString())


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