public class Statistics extends JApplet {
JTextArea Xout = new JTextArea(20, 50);
public void init() {
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(new JLabel("Statistics!"));
cp.add(new JScrollPane(Xout));
HashMap hm = new HashMap();
for(int i = 0; i < 20000000; i++) {
// Produce a number between 0 and 20:
Integer r =
new Integer((int)(Math.random() * 20));
if(hm.containsKey(r))
((Counter)hm.get(r)).i++;
else
hm.put(r, new Counter());
}
System.out.println(hm) ;
for(int i = 0; i < 20; i++) {
Integer r = new Integer((int)(i));
if(hm.containsKey(r)){
Counter c = (Counter)hm.get(r);
Xout.append (r + " -> " + c + "\n");
}
}
}
class Counter {
int i = 1;
public String toString() {
return Integer.toString(i);
}
}
public static void main(String[] args) {
Console.run(new Statistics(), 600, 400);
}
} ///:~