Java - Interview Questions and Answers for 'Hashmap' | Search Java Interview Question - javasearch.buggybread.com
Javasearch.buggybread.com
Share

Search Java Interview Questions


 2098 questions in repository.
 There are more than 200 unanswered questions.
Click here and help us by providing the answer.
Label / Company      Label / Company / Text

   



Interview Questions and Answers for 'Hashmap' - 21 question(s) found - Order By Newest

Advanced level question frequently asked in US based companies. Recently asked in EMC and Intuit.
  Q1. Can you provide some implementation of a Dictionary having large number of words ? Solution
Admin
info@buggybread.com
Ans. Simplest implementation we can have is a List wherein we can place ordered words and hence can perform Binary Search.

Other implementation with better search performance is to use HashMap with key as first character of the word and value as a LinkedList.

Further level up, we can have linked Hashmaps like ,

hashmap {
a ( key ) -> hashmap (key-aa , value (hashmap(key-aaa,value)
b ( key ) -> hashmap (key-ba , value (hashmap(key-baa,value)
....................................................................................
z( key ) -> hashmap (key-za , value (hashmap(key-zaa,value)
}

upto n levels ( where n is the average size of the word in dictionary.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   collections   hashmap   binary search   search algorithm   advanced   architecture   data structure     Dell EMC   Intuit   Corporate Brokers  PWC India  Yahoo  Oracle        frequent

Try 1 Question(s) Test


Very frequently asked. Favorite question in Walk in Drive of many Indian service companies.
  Q2. Difference between TreeMap and HashMap ?Core Java
Anonymous
Ans. They are different the way their elements are stored in memory. TreeMap stores the Keys in order whereas HashMap stores the key value pairs randomly.

  Sample Code for treemap

  Sample Code for hashmap

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   collections   map   treemap   hashmap     Amber Road  CTS (Cognizant)  ION Trading  DataMetica  Accenture  The Advisory Board Company  BidGely  Infosys  Tata Consultancy (TCS)  HCL Technologies  Wipro  Booking.com  MakeMyTrip  Garena Online  Garmin  ION Trading  Accuity  Palantir Technologies  Books International  Wealthfront  Marin Software  Garmin  IBM  Global Logic  Fiserv  Cumulus Systems  CCTS  UST Global      basic        frequent

Try 1 Question(s) Test


Very frequently asked across all types of companies.
  Q3. Difference between HashMap and Hashtable?Core Java
Anonymous
Ans. Hashtable is synchronized whereas HashMap is not.HashMap allows null values whereas Hashtable doesnt allow null values.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   collections   hashmap   map   hashtable   basic interview question   ebay   tcs    hcl technologies   infosys technologies     Morgan Stanley  Yahoo  Q3 Technologies  PriceLine  Solution Rally  Coupang  HCL Technologies  Tata Consultancy (TCS)   Infosys Technologies  Accenture India  JPMorgan Chase  Priceline.com      basic        frequent

Try 2 Question(s) Test


 Q4. Which of the following collections stores its elements in insertion Order ?

a. HashMap
b. TreeMap
c. LinkedHashMap
d. LinkedMap
Core Java
Anonymous
Ans. LinkedHashMap

  Sample Code for LinkedHashMap

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     linkedhashmap   collections   java   map


 Q5. Why HashTable has been deprecated ?Core Java2016-07-08 10:01:58

Ans. HashTable has been Deprecated as an alternative ConcurrentHashMap has been provided which uses multiple buckets to store data and hence much better performance than hashtable. Moreover there is already a raw type hashmap. The only difference between the HashTable and HashMap is that Hashtable is synchronized whereas HashMap is not. Most of the synchronized collections have been deprecated and their raw alternative have been presented as preferred.

Synchronization has a cost. Using synchronized collection at a place where there is hardly any need of it would means useless utilization of resources. As these collections are rarely used in static context or shared among threads, Java might have thought it better to just provide the raw collection and let developer implement synchronization if he feels the need to do so.

As HashMap has been presented as default and preferred way of using Map with read optimized hashing, and ConcurrentHashMap has been provided for synchronized access which provides better performance than HashTable, Java thought it right to deprecate the use of HashTable.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     hashtable  synchronized collections  Why synchronized collections have been deprecated  Why HashTable has been deprecated  HashTable vs HashMap      expert

Try 3 Question(s) Test


 Q6. There are two objects a and b with same hashcode. I am inserting these two objects inside a hashmap.

hMap.put(a,a);
hMap.put(b,b);

where a.hashCode()==b.hashCode()

Now tell me how many objects will be there inside the hashmap?
Core Java
Anonymous
Ans. There can be two different elements with the same hashcode. When two elements have the same hashcode then Java uses the equals to further differentation. So there can be one or two objects depending on the content of the objects.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   hashcode   map   hashmap   object reference   advanced


 Q7. Can we use null as a key for a map collection ?Core Java
Anonymous
Ans. Yes , for HashMap. HashMap implements Map interface. HashMap allows one null key and any number of null values.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   collections   hashmap   map   hashtable   yes-no     BlackBerry


 Q8. Difference between Map and HashMap ?Core Java
Anonymous
Ans. Map is an interface where HashMap is the concrete class.

  Sample Code for map

  Sample Code for hashmap

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   collections   hashmap   map      basic        frequent


 Q9. Can we add duplicate keys in a HashMap ? What will happen if we attempt to add duplicate values ?
Admin
info@buggybread.com
Ans. No, We cannot have duplicate keys in HashMap. If we attempt to do so , the previous value for the key is overwritten.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   collections   hashmap   map   hashtable

Try 1 Question(s) Test



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q10. If we try to add duplicate key to the HashMap, What will happen ?

a. It will throw an exception.
b. It won't add the new Element without any exception.
c. The new element will replace the existing element.
d. Compiler will identify the problem and will throw an error.
Anonymous
Ans. The new element will replace the existing element.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     hashmap   map   collections   java


 Q11. Why String is popular HashMap key in Java?Core Java
Admin
info@buggybread.com
Ans. Since String is immutable, its hashcode is cached at the time of creation and it doesnt need to be calculated again. This makes it a great candidate for key in a Map and its processing is fast than other HashMap key objects. This is why String is mostly used Object as HashMap keys.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   string class   string   immutable   hashmap   immutable   hashcode   hash code   advanced     TD Bank  HCL Technologies      expert        frequent


 Q12. Which Java collection class can be used to maintain the entries in the order in which they were last accessed?
Admin
info@buggybread.com
Ans. LinkedHashMap

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   collections   hashmap   linkedhashmap


 Q13. What is a ConcurrentHashMap ?Core Java
Admin
info@buggybread.com
Ans. ConcurrentHashMap is a hashMap that allows concurrent modifications from multiple threads as there can be multiple locks on the same hashmap.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   collections   hashmap   map   concurrenthashmap   concurrenthashmap  concurrency   general electric   ge     Cognizant (CTS)  Synechron  General Electric  MakeMyTrip.com  Nomura Securities  Xpanxion  Royal Bank of Scotland (RBS)  ProKarma  Talentica  Citi  EdgeVerve Systems  Six Dee Telecom  ReqRoute  Qumulo  Goldman Sachs  HSBC Holdings  AlertMe.com  BlackRock  Helpchat  Bally Technologies  Hubzu  Xpanxion  Coromandel Infotech  Paytm  YuMe  InfoGain  Markit  Wells Fargo  Geometric  Acloudzyn Technologies  Roposo        rare


 Q14. How is HashSet maintained in memory by Java ?Core Java
Admin
info@buggybread.com
Ans. HashSet is maintained as HashMap by Java with values of the HashSet as Keys of the HashMap and value of the HashMap as the constant PRESENT.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     ebay  java   collections   hashset   hashmap      expert


 Q15. Difference between HashMap and WeakHashMap ?
Admin
info@buggybread.com
Ans. WeakHashMap uses weak reference for keys, which means if the key object doesn't have any reference then both key/value mapping will become eligible for garbage collection.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     java   collections   hashmap   weakhashmap   difference between   garbage collection   ebay


 Q16. Which of the following is false ?

a. HashMap came before HashTable.
b. HashMap allows null values whereas Hashtable doesn’t allow null values.
c. HashTable and HashMap allow Key-Value pairs.
d. Hashtable is synchronized whereas HashMap is not.
Anonymous
Ans. HashMap came before HashTable.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     map   hashmap   hashtable   collection   java

Try 2 Question(s) Test


 Q17. What is IdentityHashMap ?Core Java2016-08-04 14:34:21

Ans. IdentityHashMap is a class that implements AbstractMap and provides a data structure with Elements having Key Value pair, just like HashMap. It is similar to HashMap except that it uses reference equality when comparing elements.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     IdentityHashMap  map  hashmap  collections


Frequently asked in high end product companies.
 Q18. How is Hashmap internally implemented in Java ?Core Java2016-12-06 08:49:37

Ans. https://www.quora.com/How-is-Hashmap-in-Java-implemented-internally-What-are-the-pros-and-cons-to-use-it-What-are-the-complexities-it-provides-for-insert-delete-and-lookup/answer/Anshudeep-Bajpai?srid=iwnK

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     hashmap  collections  hashmap internal implementation     Veritas  Sophos  Salesforce  United Health Group (UHG)  Priceline.com  Roposo  virtusa polaris  Tata Consultancy (TCS)  Hexaware Technologies  Shiksha Infotech      Intermediate


 Q19. Difference between concurrentHashMap and HashTable ?Core Java2017-01-29 18:37:04

Ans. HashTable locks the complete collection to provide synchronization whereas ConcurrentHashMap only locks segments to achieve synchronization and hence better efficient and faster.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     concurrentHashMap  HashTable  Map  Collections     Talentica  BlackRock



Do you think these are the Best Java Frameworks ?

OpenXavaSPRING MVCApache StripesCheck everything
that is Best in Java

Click Here



 Q20. isnt the use of HashTable and ConcurrentHashMap the same, i.e providing synchronized map collection ?Core Java2017-01-29 18:38:52

Ans. Yes, they both aim at providing synchronized access to the Map collection. The only difference is in their implementation. ConcurrentHashMap came after HashTable and hence technically more efficient as it doesn't lock the complete map while accessing it.

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     HashTable  ConcurrentHashMap  map  Collections


 Q21. If a Map needs to be accessed simultaneously by multiple threads, which collection class should be used ? Core Java2017-08-17 11:01:07

Ans. ConcurrentHashMap

 Help us improve. Please let us know the company, where you were asked this question :   

   Like      Discuss      Correct / Improve     map  concurrenthashmap  multithreading




Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner



comments powered by Disqus
 

Help us and Others Improve. Please let us know the questions asked in any of your previous interview.

Any input from you will be highly appreciated and It will unlock the application for 10 more requests.

Company Name:
Questions Asked:
         

X Close this

X Close this

Help Us Improve.
Please share your
interview experience.

Company Name:   


Questions Asked: