Skip to main content
The 2024 Developer Survey results are live! See the results
8 votes

Returning Groovy class fields as a map

I took inspiration from @tim_yates answer and defined a Mappable trait which handles nested Mappable objects. The Trait (as ...
rsilva4's user avatar
  • 181
7 votes

Groovy optimize method to convert JSON to map

In order to convert String to Map, use JsonSlurper: new JsonSlurper().parseText(requestString)
Bogdan Kobylynskyi's user avatar
4 votes
Accepted

Converting regular text phrase to Pig Latin in Groovy

General Strings In Groovy you can use single quotes for "regular" strings. Documentation buffer = "" // equivalent to buffer = '' It's unlike Java where ...
Captain Man's user avatar
4 votes

Preventing XSS attacks in a Spring MVC application controller

You can use OWASP's ESAPI jar json = ESAPI.encoder().canonicalize(json); json = ESAPI.encoder().encodeForHTML(json); Dependency : ...
vaquar khan's user avatar
4 votes

Combine lists of objects with no duplicates

Formulate your SQL query such that you get exactly the combined results you want. Don't use Groovy to do work that the database should be doing for you. It will be faster and easier.
200_success's user avatar
3 votes

Groovy script to get average price per group from different data arrays

This is already old and your coding skills have probably improved enough since then that you no longer need a review, but I just couldn't resist revising the code. May it help others appreciate Groovy....
IWilms's user avatar
  • 226
3 votes

Is this FizzBuzz code idiomatic Groovy?

Referring to 'Could I make use of any Groovy-specific features?': You could use a Category to add a isMultipleOf() method to Integer. So you could write ...
Stefan Rother-Stübs's user avatar
3 votes
Accepted

Find Invalid User Relationships in User to Manager Map (validate map as valid tree structure with no circular relationships)

Using TreeMap? You have a tree represented in a HashMap, which is completely fine. A TreeMap has a specific tree structure based ...
IWilms's user avatar
  • 226
3 votes

Design - prevent multiple nesting in function

This is the kind of code I'm having a hard time with myself, so I played around and tried different approaches. else closer to if...
IWilms's user avatar
  • 226
3 votes
Accepted

Design - prevent multiple nesting in function

Use optional instead of null. I wrote an article about avoiding nulls if you are interested. Use early returns to avoid nesting ...
shanif's user avatar
  • 705
2 votes
Accepted

Extract attachment between MIME boundary markers

Optimization for performance is not the main concern I have with this code. Rather, the problem is that extracting an attachment with ad hoc string manipulation is a fragile hack. This is a common ...
200_success's user avatar
2 votes

Finding duplicate invoices

Putting the JSONArray aside (given I don't know the fully qualified name of the class), here's a Groovy-er version: Working example ...
Emmanuel Rosa's user avatar
2 votes

Jenkins Declarative pipeline for multi-branch pipleline

I have found an inherent flaw in the backup and copy part. Namely ...
JoSSte's user avatar
  • 199
2 votes
Accepted

Replace each element in a list

To replace the elements in-place without building a new list, you would need to reassign the elements at each index, that is, essentially ...
janos's user avatar
  • 111k
2 votes

Spring: Creating a Mutable Common Command Object or Controller Interceptor

I don't like the idea of modeling a common set of properties (or as you say "... for just about every API call..." probably a mostly common set of properties) as a superclass. IMO inheritance should ...
mtj's user avatar
  • 4,822
1 vote

Groovy script to get average price per group from different data arrays

After being investigating a little bit more about usage of closures and some other facilities that Groovy provides, I managed to reduce a little bit the algorithm, after looking for benchmarking posts,...
Marcelo Tataje's user avatar
1 vote

Replace each element in a list

zorglubs = zorglubs.collect{it.transform()} will a bit more elegantly but actually collect returns new array.
sintasy's user avatar
  • 111
1 vote
Accepted

Is this FizzBuzz code idiomatic Groovy?

You could write a straightforward method: private boolean isMultipleOf3(number) { isMultipleOf(3, number) } Or you could define it as a partial method: <...
200_success's user avatar
1 vote

Combine lists of objects with no duplicates

To provide a Groovy related answer: There is a method groupBy() on Iterables, which is one of my favourites. You could merge the lists into one list, for example ...
IWilms's user avatar
  • 226
1 vote

More groovifed way of sublist with varying range

I like the code already. If you want to get rid of the increments you could use studentNodes.collate(20) to create chunks of length ...
IWilms's user avatar
  • 226
1 vote

Groovy method optimization to build <String, List<List>> tokens

Extending to Emmanuel Rosa's answer, drop(2) can be used to skip headers. The advantage is that we can use ...
Ashish's user avatar
  • 211

Only top scored, non community-wiki answers of a minimum length are eligible