Simple demonstration of HashMap : HashTable Map : Collections Data Structure : Java examples (example source code) Organized by topic

Java
C++
PHP
Java Home »  Collections Data Structure   » [  HashTable Map  ]  Screenshots 
 



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))
        ((Counterhm.get(r)).i++;
      else
        hm.put(r, new Counter());
    }
    System.out.println(hm);
  }
///:~



           
       
Related examples in the same category
1.  Associates keys with values Associates keys with values
2.  A simple Map implementation A simple Map implementation
3.  Hash table with separate chaining
4.  Hash table with linear probing Hash table with linear probing
5.  Hash table with double hashing Hash table with double hashing
6.  Working with Key-Value Pairs in a Hashtable
7.  Demonstrate the Hashtable class, and an Enumeration
8.  Demonstrate the HashMap class, and an Iterator Demonstrate the HashMap class, and an Iterator
9.  Soft HashMap
10.  MultiMap extends AbstractMap
11.  Array Map extends AbstractMap Array Map extends AbstractMap
12.  Demonstrating the WeakHashMap Demonstrating the WeakHashMap
13.  Use treemap Use treemap
14.  Sorting Elements in a TreeMap Sorting Elements in a TreeMap
15.  What you can do with a TreeMap What you can do with a TreeMap
16.  A Map implemented with ArrayLists








Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.