0% found this document useful (0 votes)
45 views7 pages

Collection Fram Work in Java Interview Questions

The document discusses the Collection framework in Java, including differences between various collection classes and interfaces. It covers Array vs Collection, Collection interfaces like List and Set, differences between ArrayList and Vector, ArrayList and LinkedList, Iterator and ListIterator, List and Set, HashSet and TreeSet, Set and Map, HashSet and HashMap, HashMap and TreeMap, HashMap and Hashtable.

Uploaded by

Partha hr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views7 pages

Collection Fram Work in Java Interview Questions

The document discusses the Collection framework in Java, including differences between various collection classes and interfaces. It covers Array vs Collection, Collection interfaces like List and Set, differences between ArrayList and Vector, ArrayList and LinkedList, Iterator and ListIterator, List and Set, HashSet and TreeSet, Set and Map, HashSet and HashMap, HashMap and TreeMap, HashMap and Hashtable.

Uploaded by

Partha hr
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 7

COLLECTION FRAMEWORK IN JAVA

1) What is the Collection framework in Java?


Collection Framework is a combination of classes and interface, which is used to store and manipulate the data in the form of
objects. It provides various classes such as ArrayList, Vector, Stack, and HashSet, etc. and interfaces such as List, Queue,
Set, etc. for this purpose.

2) What are the main differences between array and collection?


Array and Collection are somewhat similar regarding storing the references of objects and manipulating the data, but they
differ in many ways. The main differences between the array and Collection are defined below:

*Arrays are always of fixed size, i.e., a user can not increase or decrease the length of the array according to their
requirement or at runtime, but In Collection, size can be changed dynamically as per need.

*Arrays can only store homogeneous or similar type objects, but in Collection, heterogeneous objects can be stored.

*Arrays cannot provide the ?ready-made? methods for user requirements as sorting, searching, etc. but Collection
includes readymade methods to use.

3) Explain various interfaces used in Collection framework?


Collection framework implements various interfaces, Collection interface and Map interface (java.util.Map) are the mainly
used interfaces of Java Collection Framework. List of interfaces of Collection Framework is given below:

1. Collection interface: Collection (java.util.Collection) is the primary interface, and every collection must implement this
interface.

Syntax:

1. public interface Collection<E>extends Iterable


Where <E> represents that this interface is of Generic type

2. List interface: List interface extends the Collection interface, and it is an ordered collection of objects. It contains
duplicate elements. It also allows random access of elements.

Syntax:

1. public interface List<E> extends Collection<E>


3. Set interface: Set (java.util.Set) interface is a collection which cannot contain duplicate elements. It can only include
inherited methods of Collection interface

Syntax:

1. public interface Set<E> extends Collection<E>


Queue interface: Queue (java.util.Queue) interface defines queue data structure, which stores the elements in the form FIFO
(first in first out).

Syntax:

1. public interface Queue<E> extends Collection<E>


4. Dequeue interface: it is a double-ended-queue. It allows the insertion and removal of elements from both ends. It
implants the properties of both Stack and queue so it can perform LIFO (Last in first out) stack and FIFO (first in first out)
queue, operations.
Syntax:

1. public interface Dequeue<E> extends Queue<E>


5. Map interface: A Map (java.util.Map) represents a key, value pair storage of elements. Map interface does not implement
the Collection interface. It can only contain a unique key but can have duplicate elements. There are two interfaces which
implement Map in java that are Map interface and Sorted Map.

4) What is the difference between ArrayList and Vector?


No. ArrayList Vector
1) ArrayList is not synchronized. Vector is synchronized.

2) ArrayList is not a legacy class. Vector is a legacy class.

3) ArrayList increases its size by 50% of the array size. Vector increases its size by doubling the array size.

ArrayList is not ?thread-safe? as it is not Vector list is ?thread-safe? as it?s every method is
4)
synchronized. synchronized.

5) What is the difference between ArrayList and LinkedList?


No. ArrayList LinkedList
1) ArrayList uses a dynamic array. LinkedList uses a doubly linked list.

ArrayList is not efficient for manipulation


2) LinkedList is efficient for manipulation.
because too much is required.

3) ArrayList is better to store and fetch data. LinkedList is better to manipulate data.

4) ArrayList provides random access. LinkedList does not provide random access.

ArrayList takes less memory overhead as it LinkedList takes more memory overhead, as it stores the object as
5)
stores only object well as the address of that object.

6) What is the difference between Iterator and ListIterator?


Iterator traverses the elements in the forward direction only whereas ListIterator traverses the elements into forward and
backward direction.

No. Iterator ListIterator


The Iterator traverses the elements in the forward ListIterator traverses the elements in backward and forward
1)
direction only. directions both.

2) The Iterator can be used in List, Set, and Queue. ListIterator can be used in List only.

The Iterator can only perform remove operation ListIterator can perform ?add,? ?remove,? and ?set? operation
3)
while traversing the collection. while traversing the collection.
7) What is the difference between Iterator and Enumeration?
No. Iterator Enumeration
1) The Iterator can traverse legacy and non-legacy elements. Enumeration can traverse only legacy elements.

2) The Iterator is fail-fast. Enumeration is not fail-fast.

3) The Iterator is slower than Enumeration. Enumeration is faster than Iterator.

The Iterator can perform remove operation while The Enumeration can perform only traverse operation
4)
traversing the collection. on the collection.

8) What is the difference between List and Set?


The List and Set both extend the collection interface. However, there are some differences between the both which are listed
below.

The List can contain duplicate elements whereas Set includes unique items.

The List is an ordered collection which maintains the insertion order whereas Set is an unordered collection which
does not preserve the insertion order.

The List interface contains a single legacy class which is Vector class whereas Set interface does not have any
legacy class.

The List interface can allow n number of null values whereas Set interface only allows a single null value.

9) What is the difference between HashSet and TreeSet?


The HashSet and TreeSet, both classes, implement Set interface. The differences between the both are listed below.

HashSet maintains no order whereas TreeSet maintains ascending order.

HashSet impended by hash table whereas TreeSet implemented by a Tree structure.

HashSet performs faster than TreeSet.

HashSet is backed by HashMap whereas TreeSet is backed by TreeMap.

10) What is the difference between Set and Map?


The differences between the Set and Map are given below.

Set contains values only whereas Map contains key and values both.

Set contains unique values whereas Map can contain unique Keys with duplicate values.

Set holds a single number of null value whereas Map can include a single null key with n number of null values.
11) What is the difference between HashSet and HashMap?
The differences between the HashSet and HashMap are listed below.

HashSet contains only values whereas HashMap includes the entry (key, value). HashSet can be iterated, but
HashMap needs to convert into Set to be iterated.

HashSet implements Set interface whereas HashMap implements the Map interface

HashSet cannot have any duplicate value whereas HashMap can contain duplicate values with unique keys.

HashSet contains the only single number of null value whereas HashMap can hold a single null key with n
number of null values.

12) What is the difference between HashMap and TreeMap?


The differences between the HashMap and TreeMap are given below.

HashMap maintains no order, but TreeMap maintains ascending order.

HashMap is implemented by hash table whereas TreeMap is implemented by a Tree structure.

HashMap can be sorted by Key or value whereas TreeMap can be sorted by Key.

HashMap may contain a null key with multiple null values whereas TreeMap cannot hold a null key but can have
multiple null values.

13) What is the difference between HashMap and Hashtable?


No. HashMap Hashtable
1) HashMap is not synchronized. Hashtable is synchronized.

HashMap can contain one null key and multiple null


2) Hashtable cannot contain any null key or null value.
values.

HashMap is not ?thread-safe,? so it is useful for non- Hashtable is thread-safe, and it can be shared between
3)
threaded applications. various threads.

4) 4) HashMap inherits the AbstractMap class Hashtable inherits the Dictionary class.

14) What is the difference between Collection and Collections?


The differences between the Collection and Collections are given below.

The Collection is an interface whereas Collections is a class.

The Collection interface provides the standard functionality of data structure to List, Set, and Queue. However,
Collections class is to sort and synchronize the collection elements.

The Collection interface provides the methods that can be used for data structure whereas Collections class
provides the static methods which can be used for various operation on a collection.
15) What is the difference between Comparable and Comparator?
No. Comparable Comparator
The Comparator provides multiple sorts of
1) Comparable provides only one sort of sequence.
sequences.

2) It provides one method named compareTo(). It provides one method named compare().

3) It is found in java.lang package. It is located in java.util package.

If we implement the Comparable interface, The actual class is


4) The actual class is not changed.
modified.

16) What do you understand by BlockingQueue?


BlockingQueue is an interface which extends the Queue interface. It provides concurrency in the operations like retrieval,
insertion, deletion. While retrieval of any element, it waits for the queue to be non-empty. While storing the elements, it
waits for the available space. BlockingQueue cannot contain null elements, and implementation of BlockingQueue is thread-
safe.

Syntax:

1. public interface BlockingQueue<E> extends Queue <E>

17) What does the hashCode() method?


The hashCode() method returns a hash code value (an integer number).

The hashCode() method returns the same integer number if two keys (by calling equals() method) are identical.

However, it is possible that two hash code numbers can have different or the same keys.

If two objects do not produce an equal result by using the equals() method, then the hashcode() method will provide the
different integer result for both the objects.

18) Why we override equals() method?


The equals method is used to check whether two objects are the same or not. It needs to be overridden if we want to check
the objects based on the property.

For example, Employee is a class that has 3 data members: id, name, and salary. However, we want to check the equality of
employee object by the salary. Then, we need to override the equals() method.
19) What is the advantage of the generic collection?
There are three main advantages of using the generic collection.

If we use the generic class, we don't need typecasting.

It is type-safe and checked at compile time.

Generic confirms the stability of the code by making it bug detectable at compile time.

20) What is hash-collision in Hashtable and how it is handled in Java?


Two different keys with the same hash value are known as hash-collision. Two separate entries will be kept in a single hash
bucket to avoid the collision. There are two ways to avoid hash-collision.

Separate Chaining

Open Addressing

21) What is the Dictionary class?


The Dictionary class provides the capability to store key-value pairs.

22) What do you understand by fail-fast?


The Iterator in java which immediately throws ConcurrentmodificationException, if any structural modification occurs in, is
called as a Fail-fast iterator. Fail-fats iterator does not require any extra space in memory.

23) What is the difference between Array and ArrayList?


The main differences between the Array and ArrayList are given below.

Array ArrayList
SN
The Array is of fixed size, means we cannot resize the ArrayList is not of the fixed size we can change the size
1
array as per need. dynamically.

2 Arrays are of the static type. ArrayList is of dynamic size.

ArrayList cannot store the primitive data types it can only


3 Arrays can store primitive data types as well as objects.
store the objects.

24) In ArrayList how to inserting a element .


Inserting Element:-
Import java.util.ArrayList;
class javaCollections {
public static void main(String[] args) {
ArrayList<String> Name = new ArrayList<String>();
Name.add(“Ankit”);
Name.add(“Ankush”);
Name.add(“Altaf”);
System.out.println(Name);
}
}

You might also like