Gadgets

Friday, 22 January 2016

Collection Framework interview questions and answers

Collection framework topic is most important for interview, most of the interviewers compulsory ask at least few questions from collection framework. Collection interface most important topic in core java, we use collection topic in most of the java applications.
1) What are the limitation of object Arrays?
An array is an indexed collection of fixed number of homogeneous data elements.
Limitations of Object Arrays are fixed in size i.e. once we created an array there is no chance of increasing or decreasing its size based on our requirement. Arrays can hold only homogeneous data elements.
Example:
Student [] s = new Student [1000];
s[0] = new Student;
s[1] = new Integer(10); //compile time error : incompatible types
s[2] = “java”; // compile time error : incompatible types 
We can resolve this problem by using Object type Arrays.
Object [] o = new Object [600];
o[0] = new Student;
o[1] = new Integer(10);
o[2] = “abc”; 
Object type Arrays can hold heterogeneous data elements.There is no underlying Data Structure for arrays i.e. readymade method support is not available. For every requirement we have to write code explicitly which is increase complexity of the programmer.  
2) What are the difference between array and collections?

Array:

  • Arrays are fixed in size.
  • Array can hold only homogeneous data elements.
  • There is no underlying Data Structure for arrays
  • Array can hold both primitive and object types
  • With respect to memory arrays are not good
  • Performance wise arrays are good
Collections:
  • Collections are growable in size
  • Collections can hold both homogeneous and heterogeneous data elements
  • Every collection class is implemented based on some standard data structure
  • Collections can hold only Object types but not primitives
  • With respect to memory collections are good
  • Performance wise collections are not good

3) What are the difference between arrays and ArrayList?
Arrays:
  • Arrays are fixed in size.
  • Array can hold only homogeneous data elements.
  • There is no underlying Data Structure for arrays.
  • Array can hold both primitive and object types.

int[ ] x=new int[5];
x[0]=10; x[1]=20; x[2]=30; x[3]=40;
x[4]=”javatech”; //compile time error : incompatible types
 
ArrayList:
  • ArrayList is growable in size.
  • ArrayList allow Hetrogeneous data elements.
  • Underlying Data Structure is Resizable Array or Growable Array.
  • ArrayList can hold object types only.

ArrayList al=new ArrayList();
al.add(10); // allow integer
al.add(“A”); //allow string
 
4) What are the difference between ArrayList and vector?
ArrayList:
  • Every method present inside ArrayList is non-synchronized.
  • At a time multiple threads are allowed and it is not thread safe.
  • Relatively performance is high because threads are not required to wait.

Vector:
  • Every method present inside Vector is synchronized.
  • At a time one thread only allowed and it is thread safe.
  • Relatively performance wise is low because threads are required to wait.

5) How to get synchronized version of ArrayList?
By default ArrayList is non-synchronized but we can get synchronized version of ArrayList by using synchronizedList() method of Collections class.
ArrayList al=new ArrayList();
List l=Collections.synchronizedList(al);

6) What are the difference between Collection and Collections?
Collection:
Collection is a root level interface of the Collection Framework. List, Set, and Queue are main sub interfaces of Collection interface.
Collections:
Collections is a utility class in java.util package, it contains several utility methods for operate Collection objects.
7) Explain about Collection interface?





8) What is the difference between List, Set and Queue?
List:
  • We can represent group of individual objects as a single entity.
  • Duplicates are allowed.
  • Insertion order is preserved.

Set:
  • We can represent group of individual objects as a single entity.
  • Duplicates are not allowed.
  • Insertion order is not preserved.

Queue:
Queue interface is child interface of Collection interface. If we want to represent a group of individual objects prior to processing.
9) What is the difference between LinkedList, ArrayList and Vector?
LinkedList
ArrayList
Vector
Heterogeneous Objects are allowed.
Heterogeneous Objects are allowed.
Heterogeneous Objects are allowed.
Insertion order is preserved.
Insertion order is preserved.
Insertion order is preserved.
Duplicate Objects are allowed.
Duplicate Objects are allowed.
Duplicate Objects are allowed.
Null insertion is possible.
Null insertion is possible.
Null insertion is possible.
Underlying data structure is DoubleLinkedList.
Underlying data structure is Resizable Array or Growable Array.
Underlying data structure is Resizable Array or Growable Array.
LinkedList class implements Serializable and Cloneable interfaces.
ArrayList class implements RandomAccess.

Vector class implements RandomAccess, Serializable and Cloneable interfaces.
Best choice for insertion and deletion operation.
Best choice for retrieval operation.
Best choice for retrieval operation.

ArrayList is non-synchronized.
Vector is synchronized.

10) What is the difference between HashSet and LinkedHashSet
HashSet
LinkedHashSet
Heterogeneous objects are allowed
Heterogeneous objects are allowed
Duplicates are not allowed
Duplicates are not allowed
Null insertion is possible
Null insertion is possible
Insertion order is not preserved and it is based on hashcode of objects
Insertion order is preserved
HashSet implements Serializable and Cloneable interfaces
HashSet implements Serializable and Cloneable interfaces
Best choice for searching operation
Best choice for insertion operation
Underlying data structure is HashTable
Underlying data structure is HashTable and LinkedList

11) What is the difference between SortedSet and TreeSet?
SortedSet
TreeSet
It is an interface
It is a class
Heterogeneous objects are allowed
Heterogeneous objects are not allowed
Duplicates are not allowed
Duplicates are not allowed
Default sorting order is ascending order
Insertion is based on some sorting order

12) What is the difference between PriorityQueue and PriorityBlockingQueue?
PriorityQueue
PriorityBlockingQueue
Insertion order is not preserved, it is based on some sorting order
Insertion based on sorting order
Duplicate objects are not allowed
Duplicate objects are not allowed
Null insertion is not possible.
Null insertion is not possible.

13) What is the difference between three cursors (Enumeration, Iterator, and ListIterator)?
Property
Enumeration
Iterator
ListIterator
Applicable for
Only legacy classes
Any collection object
Only for List objects
Moment

One direction (forward)
One direction (forward)
Bi-direction (forward and backward)
Accessibility permissions
Only read
Read and remove
Read, remove, replace and addition
How to get

By using element() of vector class
By using iterator() of Collection interface
listIterator() of List interface
Methods

hasMoreElements() and nextElements()

hasNext(), next() and remove()
hasNext(), next(), nextIndex(), hasPrevious(), previous(), previousIndex(),
public void remove()
public void set() and public void add()
It is legacy
Yes(1.0 version)
No (1.2 version)
No (1.2 version)

14) What is the difference between Comparable and Comparator?
Comparable:
  • It is meant for default natural sorting order.
  • Present in java.lang
  • Contain only one method compareTo()
  • All wrapper classes and string class implements comparable interface

Comparator:
  • It is meant for customized sorting order
  • Present in java.util
  • Contains two methods compare() and equals()
  • Collector and RuleBasedCollector classes implements Comparator

15) What is Map interface?
Map is an interface, Map interface represent a group of objects as key-value pairs. Each key-value pair is called “entry”.
Key and values are objects, duplicate keys are not allowed but duplicate values can allow Map interface.
Example:

ID - key
Name - value
1201
Naren
1202
Vamshi
1203
Arun 




16) What is Entry interface?
Each key value pair is called an Entry, without existing Map object there is no chance of existing Entry Object. “Entry” interface is defined inside “Map” interface.
interface Map{
       interface Entry{
                     getKey();
                     getValue();
      }
}

17) What is the HashMap?
  • Heterogeneous objects are allowed for both key and value.
  • Insertion order is not preserved and it is based on hashcode of keys
  • Underlying data structure is HashTable
  • Duplicate keys are not allowed and duplicate values are allowed
  • Null is allowed for key only once and null is allowed for values any number of times
  • Implements serializable and cloneable. Best suitable for search operation.

18) What is the difference between HashMap and LinkedHashMap?
HashMap
LinkedHashMap
Insertion order is not preserved and it is based on hashcode  of keys
Insertion order is preserved
Underlying data structure is HashTable

Underlying data structure is HashTable and LinkedList
Introduce in 1.2 version
Introduce in 1.4 version

19) What is the difference between HashMap and HashTable?
HashMap
HashTable
HashMap is non-synchronized
HashTable is synchronized
At a time multiple threads are allowed and it is not thread safe
At a time only one thread is allowed and it is thread safe
Relatively performance is low
Relatively performance is low
Null is allowed for both key and value
Null is not allowed for both key and value
Introduced in 1.2 version and it is non legacy 
Introduced in 1.0 version and it is legacy 


20) How to get synchronized version of HashMap?
By default HashMap is non-synchronized but we can get synchronized version of HashMap by using synchronizedMap() method of Collections class.
HashMap hm=new HashMap();
Map m=Collections.synchronizedMap(hm);
 
21) What is the difference between HashMap and IdentityHashMap?
HashMap
IdentityHashMap
In HashMap .equals() method is used to identify duplicate keys
In IdentityHashMap == operator is use to identify duplicate keys
.equals() method is meant for content comparison
== operator meant for reference comparison
HashMap hm=new HashMap();
Integer i1=new Integer(10);
Integer i2=new Integer(10);
hm.put(i1, "java");
hm.put(i2, "HTML");
System.out.println(i1.equals(i2)); 
It returns true
same program but returns false 
System.out.println(i1==i2); 
It return false

22) What is SortedMap?
SortedMap is child interface of Map interface, it is represent group of key-value pairs according to some sorting order of keys. Sorting order is based on key.
23) What is TreeMap?
  • TreeMap class underlying data structure is RedBlock tree.
  • Duplicate keys are not allowed but duplicate values are allowed.
  • Insertion order is not preserved and it is based on some sorting order of keys.
  • Null key is not allowed for non empty TreeMap. Null key is allowed for empty TreeMap.
  • From 1.7version on words null key is not allowed even for empty TreeMap.

24) What is the difference between HashMap and LinkedHashMap?
HashMap
TreeMap
Heterogeneous objects are allowed for both key and value

Homogeneous keys allowed for default sorting order and  Heterogeneous keys allowed for our own sorting order
Insertion order is not preserved and it is based on hashcode  of keys
Insertion order is not preserved and it is based on some sorting order of keys
Underlying data structure is HashTable

Underlying data structure is RedBlock tree
Null is allowed for key only once and null  is allowed for values any number of times

Null key is not allowed for non empty TreeMap. Null key is allowed for empty TreeMap.
From 1.7 version on words null key is not allowed even for empty TreeMap

25) What is Properties class?
Properties is a child class of HashTable. In properties both key and value is string type.
The Properties class provides methods to get data from properties file and store data into properties file.
Generally if we change any code in the java source file then we should recompile, rebuild, and redeployment of application, to overcome this problem we use Properties.
If any information is changed from the properties file, you don't need to recompile the java class.
Constructor:
Properties p=new Properties(); 

 

1 comment: