Groovy vs. Google Collections: Round #1

In my last post, Dan Lewis (my friend and former coworker) responded with some counter-code from Google’s collections package. Instead of attempting to snap back with some witty technical retort, I challenged Dan to a code-off. Groovy collections vs. Google collections (in Java). Here is round one, and I’m going first with an example of finding combinations of collections. Hopefully, you can soon find Dan’s response in Google collection code-style on his blog.

Create the lists
def boys = ['Paco', 'Sven', 'Roger', 'Emelio']
def girls = ['Julia', 'Prudence', 'Lucy']

Now we have two simple lists, one of boys and one of girls. So what if these are all dancers, and we’d like to know all the combinations of dancers there could be? With groovy, this is extremely easy.

Find the combinations of dancers
def combos = [boys, girls].combinations()

Could it be any easier? This creates a list of lists, each inner list containing a possible combination:

[["Paco", "Julia"], ["Sven", "Julia"], ["Roger", "Julia"], ["Emelio", "Julia"], ["Paco", "Prudence"], ["Sven", "Prudence"], ["Roger", "Prudence"], ["Emelio", "Prudence"], ["Paco", "Lucy"], ["Sven", "Lucy"], ["Roger", "Lucy"], ["Emelio", "Lucy"]]

Now it would be nice if i could split the list up in some way, maybe keying by male or female. Groovy’s groupBy collection method is an easy way to do this. In the closure you provide, you simple return the key that you want to group the collection by, and it will return a map with that key pointing to a list of values. In the following example, because I know the first element of each combination is the boy and the second is the girl, I just specify a key by element position.

Grouping the combinations
def groupedByBoys = combos.groupBy { it[0] }
def groupedByGirls = combos.groupBy { it[1] }

Which returns these two groupings:

Grouped by boys
["Emelio":[["Emelio", "Julia"], ["Emelio", "Prudence"], ["Emelio", "Lucy"]], “Roger”:[["Roger", "Julia"], ["Roger", "Prudence"], ["Roger", "Lucy"]], “Sven”:[["Sven", "Julia"], ["Sven", "Prudence"], ["Sven", "Lucy"]], “Paco”:[["Paco", "Julia"], ["Paco", "Prudence"], ["Paco", "Lucy"]]]
Grouped by girls
["Lucy":[["Paco", "Lucy"], ["Sven", "Lucy"], ["Roger", "Lucy"], ["Emelio", "Lucy"]], “Prudence”:[["Paco", "Prudence"], ["Sven", "Prudence"], ["Roger", "Prudence"], ["Emelio", "Prudence"]], “Julia”:[["Paco", "Julia"], ["Sven", "Julia"], ["Roger", "Julia"], ["Emelio", "Julia"]]]

Beat that, Google Collections!

This entry was posted in uncategorized and tagged , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

5 Comments

  1. Posted April 5, 2008 at 8:41 am | Permalink

    I have posted a response.

  2. Posted April 5, 2008 at 9:46 am | Permalink

    In all fairness, the complete groovy code to print out the same as Dan’s is here, and it is 12 lines:

    def boys = ['Paco', 'Sven', 'Roger', 'Emelio']
    def girls = ['Julia', 'Prudence', 'Lucy']
    def combos = [boys, girls].combinations()
    println combos
    def groupedByBoys = combos.groupBy { it[0] }
    def groupedByGirls = combos.groupBy { it[1] }
    groupedByBoys.each{ key, val ->
        println "Partners of ${key}: ${val.collect{ it[1] }}"
    }
    groupedByGirls.each{ key, val ->
        println "Partners of ${key}: ${val.collect{ it[0] }}"
    }

    This prints out the following:

    [["Paco", "Julia"], ["Sven", "Julia"], ["Roger", "Julia"], ["Emelio", "Julia"], ["Paco", "Prudence"], ["Sven", "Prudence"], ["Roger", "Prudence"], ["Emelio", "Prudence"], ["Paco", "Lucy"], ["Sven", "Lucy"], ["Roger", "Lucy"], ["Emelio", "Lucy"]]
    Partners of Emelio: ["Julia", "Prudence", "Lucy"]
    Partners of Roger: ["Julia", "Prudence", "Lucy"]
    Partners of Sven: ["Julia", "Prudence", "Lucy"]
    Partners of Paco: ["Julia", "Prudence", "Lucy"]
    Partners of Lucy: ["Paco", "Sven", "Roger", "Emelio"]
    Partners of Prudence: ["Paco", "Sven", "Roger", "Emelio"]
    Partners of Julia: ["Paco", "Sven", "Roger", "Emelio"]

  3. Posted April 5, 2008 at 10:18 am | Permalink

    This is awesome. I’m looking forward to round 2!

  4. Posted April 19, 2008 at 7:25 am | Permalink

    One more neat thing about combinations that I didn’t mention: You aren’t restricted to two lists to combine. It can be as many as you want:

    def boys = ['Paco', 'Sven', 'Roger', 'Emelio']
    def girls = ['Julia', 'Prudence', 'Lucy']
    def locs = ['Milan', 'Prague', 'St. Louis', 'Toronto', 'Hong Kong', 'Los Angelos']

    def combos = [boys, girls, locs].combinations()

    produces:

    [["Paco", "Julia", "Milan"], ["Sven", "Julia", "Milan"], ["Roger", "Julia", "Milan"], ["Emelio", "Julia", "Milan"], ["Paco", "Prudence", "Milan"], ["Sven", "Prudence", "Milan"], ["Roger", "Prudence", "Milan"], ["Emelio", "Prudence", "Milan"], ["Paco", "Lucy", "Milan"], ["Sven", "Lucy", "Milan"], ["Roger", "Lucy", "Milan"], ["Emelio", "Lucy", "Milan"], ["Paco", "Julia", "Prague"], ["Sven", "Julia", "Prague"], ["Roger", "Julia", "Prague"], ["Emelio", "Julia", "Prague"], ["Paco", "Prudence", "Prague"], ["Sven", "Prudence", "Prague"], ["Roger", "Prudence", "Prague"], ["Emelio", "Prudence", "Prague"], ["Paco", "Lucy", "Prague"], ["Sven", "Lucy", "Prague"], ["Roger", "Lucy", "Prague"], ["Emelio", "Lucy", "Prague"], ["Paco", "Julia", "St. Louis"], ["Sven", "Julia", "St. Louis"], ["Roger", "Julia", "St. Louis"], ["Emelio", "Julia", "St. Louis"], ["Paco", "Prudence", "St. Louis"], ["Sven", "Prudence", "St. Louis"], ["Roger", "Prudence", "St. Louis"], ["Emelio", "Prudence", "St. Louis"], ["Paco", "Lucy", "St. Louis"], ["Sven", "Lucy", "St. Louis"], ["Roger", "Lucy", "St. Louis"], ["Emelio", "Lucy", "St. Louis"], ["Paco", "Julia", "Toronto"], ["Sven", "Julia", "Toronto"], ["Roger", "Julia", "Toronto"], ["Emelio", "Julia", "Toronto"], ["Paco", "Prudence", "Toronto"], ["Sven", "Prudence", "Toronto"], ["Roger", "Prudence", "Toronto"], ["Emelio", "Prudence", "Toronto"], ["Paco", "Lucy", "Toronto"], ["Sven", "Lucy", "Toronto"], ["Roger", "Lucy", "Toronto"], ["Emelio", "Lucy", "Toronto"], ["Paco", "Julia", "Hong Kong"], ["Sven", "Julia", "Hong Kong"], ["Roger", "Julia", "Hong Kong"], ["Emelio", "Julia", "Hong Kong"], ["Paco", "Prudence", "Hong Kong"], ["Sven", "Prudence", "Hong Kong"], ["Roger", "Prudence", "Hong Kong"], ["Emelio", "Prudence", "Hong Kong"], ["Paco", "Lucy", "Hong Kong"], ["Sven", "Lucy", "Hong Kong"], ["Roger", "Lucy", "Hong Kong"], ["Emelio", "Lucy", "Hong Kong"], ["Paco", "Julia", "Los Angelos"], ["Sven", "Julia", "Los Angelos"], ["Roger", "Julia", "Los Angelos"], ["Emelio", "Julia", "Los Angelos"], ["Paco", "Prudence", "Los Angelos"], ["Sven", "Prudence", "Los Angelos"], ["Roger", "Prudence", "Los Angelos"], ["Emelio", "Prudence", "Los Angelos"], ["Paco", "Lucy", "Los Angelos"], ["Sven", "Lucy", "Los Angelos"], ["Roger", "Lucy", "Los Angelos"], ["Emelio", "Lucy", "Los Angelos"]]

  5. Posted May 6, 2008 at 9:43 am | Permalink

    It’s Beatifull!

2 Trackbacks

  1. [...] dangertree techblog sweaty programming and disconnected gibberish « Groovy vs. Google Collections: Round #1 [...]

  2. [...] vs Google Collections. Matt on Groovy. Dan on Google [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*
Check out the latest GroovyMag to see an interview with me about the 1.1 release of GrailsUI: