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

Groovy example source code file (hash.java)

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

hashmap, hashmap, integer, integer, io, ioexception, ioexception, string, string, util

The Groovy hash.java source code

// $Id: hash.java,v 1.1 2004-05-23 05:06:51 bfulgham Exp $
// http://www.bagley.org/~doug/shootout/

// this program is modified from:
//   http://cm.bell-labs.com/cm/cs/who/bwk/interps/pap.html
// Timing Trials, or, the Trials of Timing: Experiments with Scripting
// and User-Interface Languages</a> by Brian W. Kernighan and
// Christopher J. Van Wyk.

import java.io.*;
import java.util.*;

public class hash {

    public static void main(String args[]) throws IOException {
	int n = Integer.parseInt(args[0]);
	int i, c;
	String s = "";
	Integer ii;
	// the original program used:
	// Hashtable ht = new Hashtable();
	// John Olsson points out that Hashtable is for synchronized access
	// and we should use instead:
	HashMap ht = new HashMap();

	c = 0;
	for (i = 1; i <= n; i++)
	    ht.put(Integer.toString(i, 16), new Integer(i));
	for (i = 1; i <= n; i++)
	    // The original code converted to decimal string this way:
	    // if (ht.containsKey(i+""))
	    if (ht.containsKey(Integer.toString(i, 10)))
		c++;

	System.out.println(c);
    }
}

Other Groovy examples (source code examples)

Here is a short list of links related to this Groovy hash.java 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.