Monday, February 15, 2016

Some Interview Questions

Most Important Questions were as follows:
1) Difference between == and equals()


2) What is the requirements for a object to be used as a key on hashmap


3) What is difference betwen ConcurrentMap and SynchronizedMap


4) What is difference betwen HashMap and TreeMap


5) Which collection you should use if your need to do frequent delete/insert operation?


6) Which collection you should use if your need to use if you need faster get operation?


7) How did you handled security in Spring?


8) How did you handle dynamic query in Mybatis?


9) What is the differenc between Rest App vs Rest Webservice


10) How did handle transaction in project?



11) How did you handle composit primary key in hibernate?


12) How did you handle many to many relationsip in hibernate?


13) What are the collections availbe in hibernate FW?


14) How did you handle optimistic locking in hibernate?


Question:

What is the requirements for a object to be used as a key on hashmap?

Ans: 


You need to override equals() and hashcode() methods of a class whose objects you want to use as Key in a hashmap... This is required because hashmap uses these 2 methods to retrieve the stored values... Read implementation of hashmap for more details... It will clear away your doubt completely.


When a class does not override the equals() or hashCode() methods, the default implementations found on the Object class are used instead. In particular, equals() simply does a check for reference equality.
That immediately explains why your approach isn't working: the new Key object clearly isn't referring to the old Key object.
If you'd like to be able to specify a new instance with the same property, then you should override the equals() method with an implementation that meets your criteria for key equality. You should override hashCode() as well, to have full control over the key comparison process.

Resource Link:

  1. https://www.quora.com/What-two-methods-do-you-need-to-implement-a-key-object-in-HashMap
  2. https://stackoverflow.com/a/9440415/2293534

3) What is difference betwen ConcurrentMap and SynchronizedMap?
---
╔═══════════════╦═══════════════════╦═══════════════════╦═════════════════════╗
║   Property    ║     HashMap       ║    Hashtable      ║  ConcurrentHashMap  ║
╠═══════════════╬═══════════════════╬═══════════════════╩═════════════════════╣ 
║      Null     ║     allowed       ║              not allowed                ║
║  values/keys  ║                   ║                                         ║
╠═══════════════╬═══════════════════╬═════════════════════════════════════════╣
║Is thread-safe ║       no          ║                  yes                    ║
╠═══════════════╬═══════════════════╬═══════════════════╦═════════════════════╣
║     Lock      ║       not         ║ locks the whole   ║ locks the portion   ║        
║  mechanism    ║    applicable     ║       map         ║                     ║ 
╠═══════════════╬═══════════════════╩═══════════════════╬═════════════════════╣
║   Iterator    ║               fail-fast               ║       fail-safe     ║ 
╚═══════════════╩═══════════════════════════════════════╩═════════════════════╝


Hashtable locks the object, while ConcurrentHashMap locks only the bucket.


ConcurrentHashMap:
=================
1. use ConcurrentHashMap. It allows concurrent modification of the Map from several threads without the need to block them.
2. Use this, if performance is critical, and each thread only inserts data to the map, with reads happening less frequently. 

Collections.synchronizedMap(map)
========
1. Collections.synchronizedMap(Map) - they use very simple synchronization, which means that only one thread can access the map at the same time.
2. As it creates a blocking Map which will degrade performance, albeit ensure consistency (if used properly).
3. Use this if you need to ensure data consistency, and each thread needs to have an up-to-date view of the map.

Resource Link: https://stackoverflow.com/questions/510632/whats-the-difference-between-concurrenthashmap-and-collections-synchronizedmap

The main difference between these two is that ConcurrentHashMap will lock only portion of the data which are being updated while other portion of data can be accessed by other threads. However, Collections.synchronizedMap() will lock all the data while updating, other threads can only access the data when the lock is released. If there are many update operations and relative small amount of read operations, you should choose ConcurrentHashMap.

Also one other difference is that ConcurrentHashMap will not preserve the order of elements in the Map passed in. It is similar to HashMap when storing data. There is no guarantee that the element order is preserved. While Collections.synchronizedMap() will preserve the elements order of the Map passed in. For example, if you pass a TreeMap to ConcurrentHashMap, the elements order in the ConcurrentHashMap may not be the same as the order in the TreeMap, but Collections.synchronizedMap() will preserve the order.

Furthermore, ConcurrentHashMap can guarantee that there is no ConcurrentModificationException thrown while one thread is updating the map and another thread is traversing the iterator obtained from the map. However, Collections.synchronizedMap() is not guaranteed on this.

1 comment:

  1. Nice article . Keep this work up. There is good hashset example visit Hashset in java

    ReplyDelete