Gourab Sarkar
Gourab Sarkar

Reputation: 80

Group by Map in Groovy

[
  programs:  [
    trip: [
      [
        description: "",
        tripName: "Niagra",
        scheduleProgramTime: "2000-01-01T00:00:00-05:00",
        country: "USA"
      ],
      [
        description: "",
        tripName: "Chennai",
        scheduleProgramTime : "2000-01-01T00:00:00-05:00",
        country: "India"
      ],
      [
        description: "",
        tripName: "Niagra",
        scheduleProgramTime: "2000-01-01T00:00:00-05:00",
        country: "USA"
      ]
    ],
    trip: [   // Editor note: this line is in error. a map cannot have two properties named "trip". In practice this compiles, but when run the SECOND "trip" wipes out the FIRST. -- @BalRog
      [
        description: "",
        tripName: "South Africa",
        scheduleProgramTime: "2000-01-01T00:00:00-05:00",
        country: "Africa"
      ],
      [
        description: "",
        tripName: "Chennai",
        scheduleProgramTime: "2000-01-01T00:00:00-05:00",
        country: "India"
      ]
    ]
  ]
]

This is my Map and I am trying to groupBy based on the tripName(Niagra/Chennai/South Africa). Output I need is like

[
  "Niagra": [
    [
      description: "",
      tripName: "Niagra",
      scheduleProgramTime: "2000-01-01T00:00:00-05:00"
      country: "USA"
    ],
    [
      description: "",
      tripName: "Niagra",
      scheduleProgramTime: "2000-01-01T00:00:00-05:00"
      country: "USA"
    ]
  ],
  "Chennai": [
    [
      description: "",
      tripName: "Chennai",
      scheduleProgramTime: "2000-01-01T00:00:00-05:00"
      country: "India"
    ]
  ],
  "South Africa": [
    [
      description: "",
      tripName: "South Africa",
      scheduleProgramTime: "2000-01-01T00:00:00-05:00"
      country: "Africa"
    ]
  ]
]

I tried using this cityMap.trip.groupBy({it.tripName}) but not getting the proper output. Thanks in advance.

I have changed it to Map. Now previously I have given curly braces instead of "[]" braces.Now based on the Map can you please provide me the details.

Upvotes: 0

Views: 1529

Answers (1)

Emmanuel Rosa
Emmanuel Rosa

Reputation: 9885

First, start with a proper Groovy Map. Your example is not a Groovy Map, however it does look like JSON. If so, you can parse it with JsonSlurper to get something Map-like. Then you can use groupBy() on the trip object:

def json = new JsonSlurper().parseText("""{
  "trip": [
    {
      "description": "",
      "tripName": "Niagra",
      "scheduleProgramTime": "2000-01-01T00:00:00-05:00",
      "country" : "USA"
    },
    {
      "description": "",
      "tripName": "Chennai",
      "scheduleProgramTime": "2000-01-01T00:00:00-05:00",
      "country" : "India"
    },
    {
      "description": "",
      "tripName": "Niagra",
      "scheduleProgramTime": "2000-01-01T00:00:00-05:00",
      "country" : "USA"
    }
    ]
}
""")

json.trip.groupBy { it.tripName }

The output looks like this:

[
    'Niagra': [
        ['country':'USA', 'description':'', 'scheduleProgramTime':'2000-01-01T00:00:00-05:00', 'tripName':'Niagra'], 
        ['country':'USA', 'description':'', 'scheduleProgramTime':'2000-01-01T00:00:00-05:00', 'tripName':'Niagra']
     ], 


    'Chennai':[
        ['country':'India', 'description':'', 'scheduleProgramTime':'2000-01-01T00:00:00-05:00', 'tripName':'Chennai']
    ],
]

Upvotes: 3

Related Questions