Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7826875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:36:33+00:00 2026-06-02T09:36:33+00:00

I came across an algorithm on the net http://www.coderanch.com/t/201836/Performance/java/Hashtable-vs-Hashmap and decided to test it

  • 0

I came across an algorithm on the net http://www.coderanch.com/t/201836/Performance/java/Hashtable-vs-Hashmap
and decided to test it

public class MapTest{
    static int sizeOfTrial = 100000;
    static String[] keys = new String[sizeOfTrial];
    static String[] vals = new String[sizeOfTrial];

    public static void main(String[] args) {
        //init sizeOfTrial key/value pairs
        for (int i=0; i < sizeOfTrial; i++){
          String s1 = "key"+ i;
          String s2 = "val"+ i;
          keys[i] = s1;
          vals[i] = s2;
        }
        test(new TreeMap(), "TreeMap");
        test(new Hashtable(), "Hashtable");
        test(new HashMap(), "HashMap");
        test(new Hashtable(200000), "Hashtable presized");
        test(new HashMap(200000), "HashMap presized");
    }

  public static void test(Map tm, String name){
    long t1 = System.currentTimeMillis();
    for (int i=0; i < sizeOfTrial; i++){
      tm.put(keys[i],vals[i]);
    }
    for (int i=0; i < sizeOfTrial; i++){
      tm.get(keys[i]);
    }
    long t2 = System.currentTimeMillis();
    System.out.println("total time for " + name + ": " + (t2-t1));
  }
}

and i got the following results

total time for TreeMap: 1744
total time for Hashtable: 446
total time for HashMap: 234
total time for Hashtable presized: 209
total time for HashMap presized: 196

Is this JVM dependent and arbitrary or does it really provide faster access and storage time?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:36:34+00:00Added an answer on June 2, 2026 at 9:36 am

    Predefining the expected size of any container-type class will provide faster storage time simply because the storage doesn’t have to be dynamically re-allocated at runtime as often. Usually the backing storage is some sort of array, and when you go beyond the available capacity, the array has to be copied into a new larger array. This is an expensive operation that may have to happen multiple times if you store a large number of objects into a container that started at a very small capacity.

    The performance of reading from the map shouldn’t be affected either way. You could demonstrate this better by timing the tm.put part separately from the tm.get part.


    Edit: To illustrate this point further, I modified the code to time tm.put separately from tm.get. Here are the results on my machine:

    total time for TreeMap tm.put: 159
    total time for TreeMap tm.get: 74
    total time for Hashtable tm.put: 20
    total time for Hashtable tm.get: 10
    total time for HashMap tm.put: 42
    total time for HashMap tm.get: 5
    total time for Hashtable presized tm.put: 11
    total time for Hashtable presized tm.get: 9
    total time for HashMap presized tm.put: 6
    total time for HashMap presized tm.get: 4
    

    Notice that the difference between Hashtable regular and presized for tm.put is a factor of ~2. SImilarly, with HashMap the difference between regular and presized is a factor of ~7 for storing. However, looking at the reading side, both Hashtable and Hashmap have roughly the same timings for tm.get in both cases (10 ms vs 9 ms for Hashtable, and 5 ms vs 4 ms for HashMap). Also notice that in the presized case, both putting and getting take roughly about the same amount of total time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across an interesting algorithm question in an interview. I gave my answer
I came across a post in SO where the algorithm is implemented in python
I came across an implementation of the nearest neighbor algorithm for finding matches between
I came across some java code for doing a prefix search on a JList.
I just installed membase and the enyim client for .NET, and came across an
I came across this sort algorithm when looking for a function to sort a
I came across the declaration in a software best practices guide that algorithm and
Possible Duplicate: Algorithm to find Lucky Numbers I came across this question.A number is
I am currently working through a book on algorithm design and came across a
While solving a geometry problem, I came across an approach called Sliding Window Algorithm.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.