Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



What is HashMap and Map?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 27-02-2009
Member
 
Join Date: Feb 2009
Posts: 27
What is HashMap and Map?
  

Hello, Everybody I am a Newbie to Java and Learning and Preparing myself for To become Java Expert , I was Stuck at a Partoculat point and Can you Please tell what is Hashmap and map, and also can you tell me what is the basic difference the hashmap and map and their significance, You can also tell any other Info Regarding this Topi Which you Feel is relevent Thanks in Advance for your replies .

Reply With Quote
  #2  
Old 27-02-2009
Member
 
Join Date: May 2008
Posts: 2,007
Re: What is HashMap and Map?

The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesnt allow). HashMap does not guarantee that the order of the map will remain constant over time. HashMap is unsynchronized and Hashtable is synchronized.
Reply With Quote
  #3  
Old 27-02-2009
Member
 
Join Date: Apr 2008
Posts: 1,947
Re: What is HashMap and Map?

A map will traverse a tree to find a match. ie log complexity A hash map will create a hash of the string, and use that hash to index an array, giving constant lookup. note: collision detection is required because a hash of a string can produce the same index as the hash of another string. Therefore complexity is added to manage collisions.

map:
  • log traverse complexity
  • string comparison at each node

hashmap
  • hash creation (might be linear complexity to string size depending on algorithm to create hash)
  • constant lookup with hash
  • with consideration to collision. which might add linear complexity to number of collisions for the same hash.
Reply With Quote
  #4  
Old 27-02-2009
Member
 
Join Date: May 2008
Posts: 115
Re: What is HashMap and Map?

HashMap
Hash table based implementation of the Map interface. This implementation provides all of the optional map operations, and permits null values and the null key. (The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls.) This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time. This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets. Iteration over collection views requires time proportional to the "capacity" of the HashMap instance (the number of buckets) plus its size (the number of key-value mappings). Thus, it's very important not to set the initial capacity too high (or the load factor too low) if iteration performance is important. An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table, and the initial capacity is simply the capacity at the time the hash table is created. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased. When the number of entries in the hash table exceeds the product of the load factor and the current capacity, the capacity is roughly doubled by calling the rehash method. As a general rule, the default load factor (.75) offers a good tradeoff between time and space costs. Higher values decrease the space overhead but increase the lookup cost (reflected in most of the operations of the HashMap class, including get and put). The expected number of entries in the map and its load factor should be taken into account when setting its initial capacity, so as to minimize the number of rehash operations. If the initial capacity is greater than the maximum number of entries divided by the load factor, no rehash operations will ever occur. If many mappings are to be stored in a HashMap instance, creating it with a sufficiently large capacity will allow the mappings to be stored more efficiently than letting it perform automatic rehashing as needed to grow the table.

Map
An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. This interface takes the place of the Dictionary class, which was a totally abstract class rather than an interface. The Map interface provides three collection views, which allow a map's contents to be viewed as a set of keys, collection of values, or set of key-value mappings. The order of a map is defined as the order in which the iterators on the map's collection views return their elements. Some map implementations, like the TreeMap class, make specific guarantees as to their order; others, like the HashMap class, do not. Note: great care must be exercised if mutable objects are used as map keys. The behavior of a map is not specified if the value of an object is changed in a manner that affects equals comparisons while the object is a key in the map. A special case of this prohibition is that it is not permissible for a map to contain itself as a key. While it is permissible for a map to contain itself as a value, extreme caution is advised: the equals and hashCode methods are no longer well defined on a such a map. All general-purpose map implementation classes should provide two "standard" constructors: a void (no arguments) constructor which creates an empty map, and a constructor with a single argument of type Map, which creates a new map with the same key-value mappings as its argument. In effect, the latter constructor allows the user to copy any map, producing an equivalent map of the desired class. There is no way to enforce this recommendation (as interfaces cannot contain constructors) but all of the general-purpose map implementations in the JDK comply. The "destructive" methods contained in this interface, that is, the methods that modify the map on which they operate, are specified to throw UnsupportedOperationException if this map does not support the operation. If this is the case, these methods may, but are not required to, throw an UnsupportedOperationException if the invocation would have no effect on the map. For example, invoking the putAll(Map) method on an unmodifiable map may, but is not required to, throw the exception if the map whose mappings are to be "superimposed" is empty. Some map implementations have restrictions on the keys and values they may contain. For example, some implementations prohibit null keys and values, and some have restrictions on the types of their keys. Attempting to insert an ineligible key or value throws an unchecked exception, typically NullPointerException or ClassCastException. Attempting to query the presence of an ineligible key or value may throw an exception, or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter. More generally, attempting an operation on an ineligible key or value whose completion would not result in the insertion of an ineligible element into the map may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as "optional" in the specification for this interface.
Reply With Quote
  #5  
Old 27-02-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: What is HashMap and Map?

Hello. The Hashtable is one of the original collection classes in Java. HashMap is part of the new Collections Framework, added with Java 2, v1.2.The key difference between the two is that access to the Hashtable is synchronized on the table while access to the HashMap isn't. You can add it, but it isn't there by default.Another difference is that iterator in the HashMap is fail-safe while the enumerator for the Hashtable isn't. If you change the map while iterating, you'll know.And, a third difference is that HashMap permits null values in it, while Hashtable doesn't.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "What is HashMap and Map?"
Thread Thread Starter Forum Replies Last Post
HashMap vs HashTable Joyjeet Software Development 6 04-01-2011 08:07 PM
Design calculator with HashMap Angelica Maria Software Development 4 25-03-2010 01:47 PM
Associated values with HashMap Miles Runner Software Development 5 13-02-2010 02:50 AM
Sorting a hashmap Gunner 1 Software Development 5 10-02-2010 04:06 AM
Iteration over a hashmap New ID Software Development 5 07-02-2010 02:40 AM


All times are GMT +5.5. The time now is 05:54 AM.