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

Groovy example source code file (wordfreq.groovy)

This example Groovy source code file (wordfreq.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

treemap, treemap, w, w

The Groovy wordfreq.groovy source code

#!/bin/env groovy
/*
	$Id: wordfreq.groovy,v 1.3 2005-09-25 20:16:20 igouy-guest Exp $

	The Great Computer Language Shootout
	http://shootout.alioth.debian.org/
 
	contributed by Jochen Hinrichsen
*/

// def dict = [:]
def dict = new TreeMap()

// read input, build dictionary
System.in.eachLine() { line ->
	// split on words
	line.split("\\W").each() { word ->
		def s = word.toLowerCase()
		def entry = dict[s]
		dict[s] = (entry == null) ? 1 : entry+1
	}
}

// default sort() is smallest first
// sort for multiple properties: [ it.value, it.key ]
assert dict != null
assert dict.values() != null
assert (dict.values().sort({ l, r -> r <=> l})) != null
dict.values().sort({ l, r -> r <=> l}).each() { value ->
/*
	assert value != null
    def entry = dict.find() { e ->
        def v = e.getValue()
		assert v != null
        e.getValue() == value
    }
	assert entry != null
*/
    // println "${value.toString().padLeft(8)} ${entry.key}"
    println "${value.toString().padLeft(8)}"
}

// EOF

Other Groovy examples (source code examples)

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

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

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 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.