home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Groovy example source code file (hash2.java)

This example Groovy source code file (hash2.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, iterator, iterator, string, string, util, val, val

The Groovy hash2.java source code

// $Id: hash2.java,v 1.1 2004-05-23 05:50:10 bfulgham Exp $
// http://www.bagley.org/~doug/shootout/

import java.util.*;

class Val {
    int val;
    Val(int init) { val = init; }
}

public class hash2 {
    public static void main(String args[]) {
	int n = Integer.parseInt(args[0]);
	HashMap hash1 = new HashMap(10000);
	HashMap hash2 = new HashMap(n);

	for(int i = 0; i < 10000; i++)
	    hash1.put("foo_" + Integer.toString(i, 10), new Val(i));
	for(int i = 0; i < n; i++) {
	    Iterator it = hash1.entrySet().iterator();
	    while(it.hasNext()) {
			Map.Entry h1 = (Map.Entry)it.next();
			String key = (String)h1.getKey();
			int v1 = ((Val)h1.getValue()).val;
			if (hash2.containsKey(key))
				((Val)hash2.get(key)).val += v1;
			else
				hash2.put(key, new Val(v1));
	    }
	}

	System.out.print(((Val)hash1.get("foo_1")).val    + " " +
	                 ((Val)hash1.get("foo_9999")).val + " " +
	                 ((Val)hash2.get("foo_1")).val    + " " +
	                 ((Val)hash2.get("foo_9999")).val + "\n");
    }
}

Other Groovy examples (source code examples)

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

new blog posts

 

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