In this Article i am trying to explore some of the frequently asked Java Collection Framework interview questions. I am sure without Collection Framework topic in Technical Interview questions there is no Technical Round for java developer. Because as a java developer should have efficient knowledge on Collection Framework to develop software project efficiently.I also have post Top 10 Exceptions in java Before attending Technical Round just brush these questions to clear your Technical Round.
1) Can you Define Collection? why we use collection in java? Tell me some benefits of Collections?
3. Tell me basic interfaces of collection framework in java?
Ans: There are mainly four types of Interfaces are there , they are
a)Collection
b)Set
c)List
d)Map
a)Collection: This interface can be used for representing a group of objects as single entity. It is root of the collection framework hierarchy.
b)Set : This can be used for representing a group of individual objects where duplicate objects are not allowed and insertion operation is not preserved. This is the child interface of collection.
c) List: This can be used for representing a group of individual objects as a single entity where insertion order is preserved and duplicate objects allowed. This is child interface of collection.
d)Map: This can be used for representing a group of objects as key value pairs. Both key and value are objects only.
Ex: Studentname --> StudentRollno
phoneno --> contact details
Map interface is not child interface of collection
4) can extend collection interface through Map Interface?If not Why?
Ans: No. Map Interface does not extend Collection and vice versa. Even though Map is the part of collection framework but their designation were different. Map Interface defines key-value pairs and it provides some rich methods like to retrieve list of keys or values as collection but it can not store as group of elements as single entity. Therefore Map interface does not extend Collection interface.
5) Why we use Iterator in java? Explain with example?
Ans: Iterator was Introduced in java 1.2 version. Iterator interface has methods to iterate over any collection. we can get iterator()method from collection class.By iterating the elements we can perform remove operation also in addition to read operation.
This interface contains the following 3 methods.
boolean hasNext();
Object next();
void remove();
Ex:
import java.util.*;
class IteratorDemo
{
public static void main(String arg[])
{
ArrayList al = new ArrayList();
for (int i =0;i<=10 ;i++ )
{
al.add(i);
}
System.out.println(al);
Iterator itr = al.iterator();
while (itr.hasNext())
{
Integer i = (Integer)itr.next();
if((i%2) == 0)
{
System.out.println(i);
}
else
{
itr.remove();
}
}
System.out.println(al );
}
}
Output:
6) can you tell me where we use enumeration in java with example?
Ans: This interface has introduced in 1.0 version it contains the following 2 methods
boolean hasMoreElements();
Object nextElement();
Ex:
import java.util.*;
class EnumaretionDemo
{
public static void main(String arg[])
{
Vector v = new Vector();
for (int i =0;i<=10 ;i++ )
{
v.addElement(i);
}
System.out.println(v);
Enumeration e = v.elements();
while (e.hasMoreElements())
{
Integer i = (Integer)e.nextElement();
if((i%2) == 0)
System.out.println(i);
}
System.out.println(v);
}
}
Output:
Limitations of Enumeration :
8) what is difference between HashMap and HashTable in java?
1) Can you Define Collection? why we use collection in java? Tell me some benefits of Collections?
Ans:
Collection is an interface available in java.util.*; package for representing group of objects into a single entity. In this interface defines the most common general methods which can be applicable for any collection object.
Note: There is no Concrete class which implements collection interface directly.
Collection Framework is group of classes and interfaces which can be used to representing collection of Objects as single entity.
Some of the benefits of Collection in java:
1. By using collection framework we can improve the code quality and speed while developing programs.
2. With Collection Framework code and software reusability is possible therefore we can reduce the programming effort.
3. Java has Provide rich set of Core collection classes by using these classes we can develop programs easily instead of implementing our own classes.
2. Can you tell me difference between Collection and Collections?
Ans: Collection is an interface available in java.util package for representing a group of objects as single entity.
Collections is an utility class available in java.util package and defines several utility methods for collection implemented class object3. Tell me basic interfaces of collection framework in java?
Ans: There are mainly four types of Interfaces are there , they are
a)Collection
b)Set
c)List
d)Map
a)Collection: This interface can be used for representing a group of objects as single entity. It is root of the collection framework hierarchy.
b)Set : This can be used for representing a group of individual objects where duplicate objects are not allowed and insertion operation is not preserved. This is the child interface of collection.
c) List: This can be used for representing a group of individual objects as a single entity where insertion order is preserved and duplicate objects allowed. This is child interface of collection.
d)Map: This can be used for representing a group of objects as key value pairs. Both key and value are objects only.
Ex: Studentname --> StudentRollno
phoneno --> contact details
Map interface is not child interface of collection
4) can extend collection interface through Map Interface?If not Why?
Ans: No. Map Interface does not extend Collection and vice versa. Even though Map is the part of collection framework but their designation were different. Map Interface defines key-value pairs and it provides some rich methods like to retrieve list of keys or values as collection but it can not store as group of elements as single entity. Therefore Map interface does not extend Collection interface.
5) Why we use Iterator in java? Explain with example?
Ans: Iterator was Introduced in java 1.2 version. Iterator interface has methods to iterate over any collection. we can get iterator()method from collection class.By iterating the elements we can perform remove operation also in addition to read operation.
This interface contains the following 3 methods.
boolean hasNext();
Object next();
void remove();
Ex:
import java.util.*;
class IteratorDemo
{
public static void main(String arg[])
{
ArrayList al = new ArrayList();
for (int i =0;i<=10 ;i++ )
{
al.add(i);
}
System.out.println(al);
Iterator itr = al.iterator();
while (itr.hasNext())
{
Integer i = (Integer)itr.next();
if((i%2) == 0)
{
System.out.println(i);
}
else
{
itr.remove();
}
}
System.out.println(al );
}
}
Output:
6) can you tell me where we use enumeration in java with example?
Ans: This interface has introduced in 1.0 version it contains the following 2 methods
boolean hasMoreElements();
Object nextElement();
Ex:
import java.util.*;
class EnumaretionDemo
{
public static void main(String arg[])
{
Vector v = new Vector();
for (int i =0;i<=10 ;i++ )
{
v.addElement(i);
}
System.out.println(v);
Enumeration e = v.elements();
while (e.hasMoreElements())
{
Integer i = (Integer)e.nextElement();
if((i%2) == 0)
System.out.println(i);
}
System.out.println(v);
}
}
Output:
Limitations of Enumeration :
- It is applicable only for legacy classes and it is not a universal cursor.
- While iterating the elements by using enumeration we can perform only read operation and we can’t perform any modify/removal operations.
- To overcome these problems we should go for Iterator interface
7) Why we use UnsupportedOperationException in collection framework?
Ans: It is an exception is used to indicate that the operations is not supported. It is mainly used in JDK classes. java.util.Collections.unmodifiablecollection when we using operations like add or remove thorws this type of exception.
8) what is difference between HashMap and HashTable in java?
Ans: HashMap:
HashMap is not sorted or ordered. If you just need a Map, and you don’t care about the order of the elements while iterating through it, you can use a HashMap. That keeps it simple. The values can be null. But the key should ne unique, so you can have one null value for key. (Allows only one null key).
HashTable:
Hashtable is almost the same as HashMap. The main differences are:
1. Hashtable does not let you have null value for key.
2. The key methods of Hashtable are synchronized. So, they may take a longer time to execute, compared to HashMap’s methods.
9) Explain TreeSet and TreeMap in java?
9) Explain TreeSet and TreeMap in java?
Ans: The underlying Data structure for the TreeSet is Balanced tree.
Duplicate objects are not allowed. If we are trying to insert duplicate object we won’t get any
compile time error or Run time error, add method simply returns false.
Insertion order is not preserved but all the elements are inserted according to some sorting order.Heterogeneous objects are not allowed, violation leads to Run time error saying class cast.
Ex:
import java.util.*;
class TreeSetDemo
{
public static void main(String arg[])
{
TreeSet t = new TreeSet();
t.add("A");
t.add("B");
t.add("Z");
t.add("L");
//t.add(new Integer(10));// ClassCastException.
//t.add(null);// NullPointerException.
t.add("A"); //false.
System.out.println(t);
}
}
null Acceptance
For the empty TreeSet as the first element null insertion is possible. But after inserting null if we are trying to insert any other element we will get NullPointerException.
If the TreeSet already contains some elements if we are trying to insert null we will get
NullPointerException.
Ex:
import java.util.*;
class TreeSetDemo
{
public static void main(String arg[])
{
TreeSet t = new TreeSet();
t.add(new StringBuffer("A"));
t.add(new StringBuffer("B"));
t.add(new StringBuffer("T"));
t.add(new StringBuffer("Z"));
System.out.println(t);
}
}
Output: Runtime Exception: ClassCastException
TreeMap:
TreeMap is a sorted Map and will be sorted by the natural order of the elements. If you want, you can define a custom sort order by means of a Comparator passed to the constructor. So, when you need a custom sort order, it is clear which class you should be using!
No comments:
Post a Comment