Solved

map in C++ v/s HashMap in Java

Posted on 2011-03-01
7
768 Views
Last Modified: 2012-05-11
Could you tell me if the time complexity of operations with a map in C++ and HashMap in Java are the same?
IF not, please tell me the time complexity of operations such as Insert/Delete/Search in a map in C++.

Thanks!
0
Comment
Question by:dshrenik
7 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 35006673
I would doubt they were any different. In theory both languages should use the best possible complexity
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 334 total points
ID: 35006744
std::map in C++ is NOT a hash table. Most implementations use a BST (binary search tree) to implement std::map, so its time complexity is quite different from that of a hash table.

For example : std::map has O(log n) search complexity for example, while a well-implemented hash table would have O(1) complexity for the same operation.
0
 

Author Comment

by:dshrenik
ID: 35006818
I'm sorry.

Can you tell if C++'s hash_map and Java's HashMap have the same time and space complexity?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 53

Expert Comment

by:Infinity08
ID: 35006854
>> Can you tell if C++'s hash_map and Java's HashMap have the same time and space complexity?

I'm not 100% sure about the Java HashMap implementation, but afaik there is a normal hash table behind it.
The std::hash_map in C++ is currently an extension, so the specific implementation depends on the implementer (but the general consensus seems to be a normal hash table).

So, yes : the complexities would be very similar, if not the same.

As always with hash tables though : a lot depends on your choice of hash function, and a proper dimensioning of the hash table (as well as collision strategy if appropriate).
0
 
LVL 33

Expert Comment

by:sarabande
ID: 35008655
can you tell about your keys? and the number of entries now and in future? is your main concern a fast read and find of existing keys or a fast insert/update/delete ?

you may think of making test implementations for either map. if you can serve realistic test data you reliably will know what is the best container for your purpose.

Sara
0
 
LVL 27

Assisted Solution

by:dpearson
dpearson earned 166 total points
ID: 35009443

Can you tell if C++'s hash_map and Java's HashMap have the same time and space complexity?

In terms of space, both will be O(n) - there is one entry in the map for each key and then a list of entries where the keys collide, but each (key,value) pair in the map will appear only once in the data structure so they're O(n) for space.

In terms of time complexity, inserts will be O(1) [locating the hashed entry is a constant time operation and adding to the end of the list of items for a key should also be a constant time operation].  Reads will be O(log n) because the process is locate the hashed entry (which is constant time) and then walk the list of items with the same hash value - which will tend to log n for a large n and a fixed map size.  (If you make your map as large as your key space then this will instead be O(1) - but that's not a typical usage pattern for maps).

C++ and Java will both have the same complexity.

Doug
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 334 total points
ID: 35009478
>> (If you make your map as large as your key space then this will instead be O(1) - but that's not a typical usage pattern for maps).

It is typical for many hash table needs to avoid collisions as much as possible though.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question