Results 1 to 1 of 1
Thread: Simple demonstration of HashMap
-
Simple demonstration of HashMap
Java Code:import java.util.HashMap; import java.util.Map; import java.util.Random; class Counter { int i = 1; public String toString() { return Integer.toString(i); } } public class Statistics { private static Random rand = new Random(); public static void main(String[] args) { Map hm = new HashMap(); for (int i = 0; i < 10000; i++) { // Produce a number between 0 and 20: Integer r = new Integer(rand.nextInt(20)); if (hm.containsKey(r)) ((Counter) hm.get(r)).i++; else hm.put(r, new Counter()); } System.out.println(hm); } }"The sole cause of man’s unhappiness is that he does not know how to stay quietly in his room." - Blaise Pascal
Similar Threads
-
Demonstration of the transient keyword
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 11:09 PM -
Demonstration of the mathematical operators
By Java Tip in forum java.utilReplies: 0Last Post: 04-16-2008, 11:02 PM -
TreeSet Demonstration
By Java Tip in forum java.langReplies: 0Last Post: 04-15-2008, 07:34 PM -
Demonstration of heaps
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:50 PM -
Ftp client demonstration
By Java Tip in forum java.netReplies: 0Last Post: 04-07-2008, 08:11 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks