File: hash2.java

package info (click to toggle)
groovy2 2.2.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 23,916 kB
  • sloc: java: 136,570; xml: 948; sh: 486; makefile: 67; ansic: 64
file content (37 lines) | stat: -rw-r--r-- 1,232 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// $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");
    }
}