Top 10 Java Collections framework interview questions and answers

1.What are the differences between ArrayList and Vector ?


ArrayList

Vector


ArrayList is not synchronized


Vector is synchronized


ArrayList is not a legacy class

Vector  is a legacy class

ArrayList increase half size of its array size when it is resized


Vector increase the double the size of its array size when it is resized


ArrayList is not thread-safe


Vector is a thread-safe

2.What is the difference between the Iterator and ListIterator ?


Iterator


ListIterator


Iterator is used to traverse the element in forward-direction only.


ListIterator is used to traverse the element in both forward and backward directions.


Iterator can use with Set,List and Queue


ListIterator can use only with List


Iterator allow only remove the element while traversing,not adding. 


ListIterator allow add,remove and modify the elements while traversing the elements.

3.What is the difference between the HashMap  and HashTable ?


HashMap




Hashtable


HashMap is not synchronized


Hashtable is synchronised


HashMap allow one null key and multiple null values


HashTable does  not allow null key and null values.


It is very useful  for the non-threaded applications

It is very useful  for threaded applications.


It does not maintain insertion order that means it does not give the guarantee in which order elements are inserted ,output will be same order.  


HashTable is a legacy class


HashMap inheritance AbstractMap class

HashMap inheritance Dictionary class

4.What is the comparable and comparator interface ?


Comparable interface


Comparator interface


Comparable interface provides the single way of sorting ie on the  basis of only one element such as id,name or mobileNumber etc. 


Comparator interface provides multiple ways of sorting ie on the basis of multiple elements such as id,name or mobileNumber etc.


We can sort the element by the CompareTo( ) method.


We can sort the element by the Compare( ) method


Comparable interface is defined  in the java.lang package.


Comparator interface is defined in the java.util package 


Comparable affects the original class 


Comparator does not affect the original class

5.What do you mean by Collection framework in java ?

Java collections framework is the collections of classes and interfaces which is used to store and manipulate the groups of objects into a single unit.There are various classes such as ArrayList,LinkedList,Vector etc and interfaces such as Set,List  and Map etc

6.What is the difference between the Set ,List and Map interface ?

Set:

It does not maintain insertion order means it depends on implementation

It does not allow duplicate element

It allows the only single null values

It does not contain legacy class

List:

It maintains the order means in which order elements are inserted,output will be in the same Order

It allows duplicate elements

It allows the many number of null values

It contains single legacy class

Map:

Map is used to store the value on the basis of key ie key-value pairs.

Map contains unique key

Map implemented class are HashMap,LinkedHashMap and TreeMap etc.

7.Write a program of storing five names using Map interface with new style.

Note:Here we will use the Map.entry( ) class which provides the two methods getKey( ) and getValue( ).

getKey( )=it is used to get key

getValue( )=It is used to  get value

import java.util.*;
import java.util.List;
import java.util.ArrayList;
public class HashMapExample
{
Public static void main(String[ ] args)
{
Map<Integer,String> obj=new HashMap<Integer,String>( );
obj.put(1,”suman”);
obj.put(2,”raj”);
obj.put(3,”ajay”);
obj.put(4,”rahul”);
obj.put(5,”kumar”);

//Traversing the elements

for(Map.entry map:obj.entrySet( ))
{
System.out.println(map.getkey( )+ “ “ +map.getValue( ));
}
}
}

Output

1 suman

2 raj

3 ajay

4 rahul

5 kumar

8.How can you convert ArrayList to Array ?

We can convert ArrayList to Array using the “toArray( )”  method.

import java.util.*;
import java.util.List;
import java.util.ArrayList;

Public  class ArrayListToArray
{
Public static void main(String[]  args)
{
List<integer> list=new ArrayList<Integer>( );
list.add(1);
list.add(2);
list.add(3);
list.add(4);

Integer [ ] array=new Integer[list.size];
array=list.toArray(array);

//Traversing the element
for(Integer a :array)
{
System.out.println(a+ “ “);
}
}
}

Output

1

2

3

4

9.What is the difference between the Collection and Collections ?

Collection

Collection is an interface.

Collection provides some interfaces such as List,Set and Map etc.

Collections:

Collections is  a class

Collections class contains some important methods such as Collections.sort( ) and Collections.synchronizedlist( ).

10.How we cam make collection thread-safe ?

Some methods are given below makes collection thread-safe

1.Collections.synchronizedList(list)

2.Collections.synchronizedSet(set)

3.Collections.synchronizedMap(map)

Thanks and Regards

Placement preparation

sumankumar80

Hello Friends,I have written and developed this website because you can learn technology easily ,prepare for the placement and also for the any type of examination . By :Suman Kumar

Leave a Reply

Your email address will not be published. Required fields are marked *