Simple demonstration of HashMap : HashTable Map « Collections Data Structure « Java






Simple demonstration of HashMap

Simple demonstration of HashMap
     
// : c11:Statistics.java
// Simple demonstration of HashMap.
// From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002
// www.BruceEckel.com. See copyright notice in CopyRight.txt.

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);
  }
} ///:~



           
         
    
    
    
    
  








Related examples in the same category

1.Check if a particular key exists in Java Hashtable
2.Check if a particular value exists in Java Hashtable
3.Get Collection of Values from Java Hashtable
4.Get Set view of Keys from Java Hashtable
5.Get Size of Java Hashtable
6.Iterate through keys of Java Hashtable
7.Remove all values from Java Hashtable
8.Scan the content of a hashtable
9.Remove value from Java Hashtable
10.Sort keys in an Hashtable
11.Associates keys with valuesAssociates keys with values
12.Iterate through values of Java Hashtable
13.A simple Map implementationA simple Map implementation
14.Hash table with separate chaining
15.Hash table with linear probingHash table with linear probing
16.Hash table with double hashingHash table with double hashing
17.Working with Key-Value Pairs in a Hashtable
18.Demonstrate the Hashtable class, and an Enumeration
19.Demonstrate the HashMap class, and an IteratorDemonstrate the HashMap class, and an Iterator
20.Soft HashMap
21.MultiMap extends AbstractMap
22.Array Map extends AbstractMapArray Map extends AbstractMap
23.Demonstrating the WeakHashMapDemonstrating the WeakHashMap
24.Use treemapUse treemap
25.Sorting Elements in a TreeMapSorting Elements in a TreeMap
26.What you can do with a TreeMapWhat you can do with a TreeMap
27.A Map implemented with ArrayLists
28.HashMap
29.Caching Hashtable
30.Hashtable that supports mostly-concurrent reading, but exclusive writing.
31.Lru Hashtable
32.Bucketized Hashtable
33.Custom hash table based on customized array