Play framework Interview Questions

How can we design a synchronized collection from giving collection?

The synchronized collection we` can use collections.synchronizedCollection (Collection c) to provide a synchronized (thread-safe) collection backed by the specified collection.

What are the other methods to iterate over a list?

The other methods iterate over a list in two different ways:

  • Using iterator
  • Using for-each loop

 

List<String> strList = new ArrayList<>();
<strong>//using for-each loop</strong>
for(String obj : strList){
    System.out.println(obj);
}
<strong>//using iterator</strong>
Iterator&lt;String&gt; it = strList.iterator();
while(it.hasNext()){
    String obj = it.next();
    System.out.println(obj);
}

 Using iterator is more thread-safe because it provides sure that if underlying list elements are changed it will throw ConcurrentModificationException.

Why do you need to use a custom object as a key in Collection classes such as the Map or Set?

When are using any custom object in Map as the key, then you need to override equals () or hashCode() method and follow their contact. In other words, when you are storing a custom object in store Collection such as SortedSet and SortedMap you can also use equals() method is consistent to compareTo () method, or those collections will not follow their contacts such as Set may provides duplicates.

What is the difference between HashMap and Hashtable in Play Framework?

A HashMap is not synchronized where as Hashtable is synchronized and HashMap is faster than Hashtable and so on.

Why do you use ConcurrentHashMap in Java?

If you have equaled the number of reader and writer than ConcurrentHashMap will perform in line of Hashtable or synchronized HashMap.

What do you understand by hashCode () and equals () methods?

A HashMap is used for key object where as hashCode() and equals() method to consider the index to out the key value pair. The hashCode () and equals () methods are also used when we try to get value from the HashMap. If hashCode() and equals() method is not define correctly, two other key’s might obtain same hashCode() and equals() result and in that situation rather than storing it at other location, the HashMpa will determine them same and overwrite them. So, we can say that, the collection classes that does not duplicate data use hashCode() equals() to determine duplicate data so these methods it is very necessary to define correctly.