OnJava8-Examples/collections/Statistics.java

26 lines
817 B
Java
Raw Normal View History

2015-12-15 11:47:04 -08:00
// collections/Statistics.java
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2016-01-25 18:05:55 -08:00
// Simple demonstration of HashMap
2015-06-15 17:47:35 -07:00
import java.util.*;
public class Statistics {
public static void main(String[] args) {
Random rand = new Random(47);
2015-09-07 11:44:36 -06:00
Map<Integer, Integer> m = new HashMap<>();
2015-06-15 17:47:35 -07:00
for(int i = 0; i < 10000; i++) {
// Produce a number between 0 and 20:
int r = rand.nextInt(20);
2016-01-25 18:05:55 -08:00
Integer freq = m.get(r); // (1)
2015-06-15 17:47:35 -07:00
m.put(r, freq == null ? 1 : freq + 1);
}
System.out.println(m);
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
{0=481, 1=502, 2=489, 3=508, 4=481, 5=503, 6=519, 7=471,
8=468, 9=549, 10=513, 11=531, 12=521, 13=506, 14=477,
15=497, 16=533, 17=509, 18=478, 19=464}
2015-09-07 11:44:36 -06:00
*/