Collections Framework Interview Questions
21 - How to Make a Map and List as Thread-Safe or Synchronized Collection?
Map syntax to make synchronized - Collections.synchronizedMap(new HashMap());List syntax to make synchronized - Collections.synchronizedList(List list);
22 - How to convert a string array to arraylist?
Use Arrays.asList() method.Example
String[ ] a = {"Hello", "Hi", "Bye"};
List list = Arrays.asList(a);
23 - What is Navigable Map and Navigable set?
The navigableMap is a sub interface of SortedMap. It is like a SortedMap. A NavigableMap may be accessed and traversed in either ascending or descending key order. It has methods decendingKeySet() and descendingmap(), headMap(), tailMap() and subMap(), ceilingKey(), floorKey(), higherKey() and lowerKey(), higherEntry(), lowerEntry(), pollFirstEntry and pollLastEntry.The navigableSet is a sub interface of SortedSet. It is like a SortedSet. It may be accessed and traversed in either ascending or descending order. It has methods -> lower, floor, ceiling, and higher return elements respectively less than, less than or equal, greater than or equal, and greater than a given element, returning null if there is no such element.
24 - What is SortedSet?
SortedSet extends Set interface. When you iterate the elements of a SortedSet the elements are returned in the sorted order. TreeSet implements SortedSet to show its elements in sorted form.By default the elements are iterated in ascending order, starting with the "smallest" and moving towards the "largest". But it is also possible to iterate the elements in descending order using the method TreeSet.descendingIterator().
25 - What do you means of Autoboxing?
Autoboxing is introduced in Java 1.5 to automatically convert primitive type into boxed object or Wrapper class or vice-versa.Example
ArrayListl=new ArrayList (); l.add(10); //Boxing l.add(new Integer(10)); //Legacy way int i=l.get(i); //UnBoxing
26 - What is anonymous inner class?
The class declared inside the body of a method without a name it is known as anonymous inner classes. These classes are very useful in some situation. For example consider a situation where you need to create the instance of an object without creating subclass of a class and also performing additional tasks such as method overloading.Syntax
button.addActionListener(new ActionListener()
{
public void actionPerfored(ActionEvent e)
{
// your code here
}
});
27 - What is Static Nested Class?
A static inner class is a class nested inside another class that has the static modifier. It is much identical to a top-level class, It has access to the private members of the class it's defined inside of.
class Outer
{
static class Inner1
{
// code
}
class Inner2
{
//code
}
}
Class Inner1 is a static inner class. Class Inner2 is an inner class that's not static. The difference between the two is that instances of the non-static inner class are permanently attached to an instance of Outer -- you can't create an Inner2 without an Outer. You can create Inner1 object independently, though.
28 - Can an ArrayList contain heterogenous objects?
Yes a ArrayList can contain heterogenous objects because a ArrayList stores everything in terms of Object.29 - What is Dictionary class?
Dictionary is an abstract class introduce in java 1.0, that stores key/value and operates much like Map. Like a Map it also stores key and value, you can store the value in a Dictionary object. Once the value is stored, you can retrieve it by using its key.30 - What is the difference between removeAll(Collection) & retainAll(Collection) method of Collection Interface?
You need to override equals() and hashCode()methods, because the removeAll and retainAll methods work by iterating the collections and comparing the existing items with the items from the passed collection via equals() method.31 - What is legacy class?
The early classes that introduced with java release i.e with jdk 1.0 and after new classes and interface is indroduces by later java release ,the early old classes are known with legacy classes. Classes and interfaces likeLegacy classes - Dictionary,Hashtable, Properties, Stack, Vector
Legacy interface - Enumeration.
32 - What is the maximum size of Integer wrapper class?
Integer wrapper maximum size is 214748364733 - What is difference between iterator access and index access?
Index based access allow access of the element directly on the basis of index whereas iterator based access has to traverse through each element to get the desired element. Traversal or search in index based datastructure is faster.34 - Does we use Arraylist in the case of multidimensional and why?
Yes, you can use ArrayList in place of multidimentional array. You need to create an ArrayList, then add an ArrayList to it as object. To add an object in to multidimentional ArrayList, you have to get the arrayList at that row, type cast it back to ArrayList as ArrayList only holds object type, then add element to arrayList. And to access the elements from the multidimensional ArrayList , you need to get the appropriate row, type cast that row to an ArrayList, then get the appropriate column and type cast that column back to appropriate object.
public static void main(String args[])
{
ArrayList multidimentional = new ArrayList();
multidimentional .add(new ArrayList());
((ArrayList) multidimentional .get(0)).add("row 0 col 0");
((ArrayList) multidimentional .get(0)).add("row 0 col 1");
multidimentional .add(new ArrayList());
((ArrayList) multidimentional .get(1)).add("row 1 col 0");
((ArrayList) multidimentional .get(1)).add("row 1 col 1");
int i = 0;
int j = 0;
// display contents of multidimentional
for(i = 0; i < multidimentional .size();i++)
{
for(j = 0; j < ((ArrayList) multidimentional .get(i)).size(); j++)
{
System.out.print( (String)((ArrayList) multidimentional .get(i)).get(j) +" ");
}
System.out.println();
}
}
35 - Difference between HashMap and TreeMap?
- Both TreeMap & HashMap are two different implementation of the Map interface.
- Both TreeMap & HashMap are non synchronized.
- TreeMap elements are stored in a tree.
- TreeMap allows us to retrieve the elements in sorted order.
- TreeMap is slower than HashMap.
- HashMap can have both keys and values as null.
36 - What is weak reference in java?
Weak reference means releasing unused objects for garbage collection can be done efficiently . When there are one or more reference to an object it will not garbage collect. But this rule depends on what type of reference it is. If an object has only weak reference associated with other objects, then it is a valid candidate for garbage collection.37 - What is the difference between SynchronizedHashMap and ConcurrentHashMap?
ConcurrentHashMap is designed for concurrency and improve performance while HashMap which is non synchronized by nature and hahsMap can be synchronized by using Collections.synchronizedMap method. ConcurrentHashMap do not allow null keys or null values while HashMap allows null keys.38 - What is the difference between shallow cloning and deep cloning in java?
- Shallow cloning - clone method of an object, creates a new instance of the same class and copies all the fields to the new instance and returns it.
- Deep cloning - When the copied object contains some other object its references are copied recursively in deep copy. serialization implements deep copy implicitly and gracefully handling cyclic dependencies.


