alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (Groovy3818Bug.groovy)

This example Groovy source code file (Groovy3818Bug.groovy) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Groovy tags/keywords

comparator, dummy, dummy, groovytestcase, han, l, luke, luke, sky, sky, skywalker, skywalker, solo, treeset

The Groovy Groovy3818Bug.groovy source code

package groovy.bugs

class Groovy3818Bug extends GroovyTestCase {
    void testCreatingSimilarSetandMapWithComparator() {
        def scompare = { a, b -> a.id <=> b.id } as Comparator

        def set = new TreeSet( scompare )

        set << [name: "Han Solo",       id: 1]
        set << [name: "Luke Skywalker", id: 2]
        set << [name: "L. Skywalker",   id: 3]

        def result = set.findAll { elt -> elt.name =~ /Sky/ }
        assert result.size() == 2

        result = set.grep { elt -> elt.name =~ /Sky/ }
        assert result.size() == 2

        def mcompare = { a, b -> a.id <=> b.id } as Comparator

        def map = new TreeMap( mcompare )

        map[[name: "Han Solo", id: 1]] = "Dummy"
        map[[name: "Luke Skywalker", id: 2]] = "Dummy"
        map[[name: "L. Skywalker",   id: 3]] = "Dummy"

        result = map.findAll { elt ->elt.key.name =~ /Sky/ }
        assert result.size() == 2
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy Groovy3818Bug.groovy source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.