0% found this document useful (0 votes)
5 views3 pages

6 Minutes CollectionFramework

The document discusses the Java Collection Framework, explaining the need for collections over arrays due to their dynamic nature. It covers various interfaces such as List, Set, and Map, detailing their characteristics and implementations like ArrayList, LinkedList, HashSet, and HashMap. Additionally, it highlights the differences between these implementations, including their handling of duplicates, ordering, and performance in operations.

Uploaded by

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

6 Minutes CollectionFramework

The document discusses the Java Collection Framework, explaining the need for collections over arrays due to their dynamic nature. It covers various interfaces such as List, Set, and Map, detailing their characteristics and implementations like ArrayList, LinkedList, HashSet, and HashMap. Additionally, it highlights the differences between these implementations, including their handling of duplicates, ordering, and performance in operations.

Uploaded by

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

Collection Framework

Ques: Why do we need collection in Java?


Ans: Arrays are not dynamic. Once an array of a particular size is declared, the size
is not modified. To add a new element in the array, a new array has to be created
with bigger size and all the elements from the old array copied to new array.
Collections are used in the situation where data is dynamic. Collections allow
adding an element, deleting an element and host of other operations. There are a
number of collections in Java allowing to choose the right collections for the right
context.

Ques: What are the important methods that are declared in collection interface?
Ans: Most important methods declared in the collection interface are the
methods to add and remove an element. Add method allows adding an element
to a collection and delete allows deleting an element from a collection.

Ques: what is List Interface?


Ans: List interface extends Collection interface. So, it contains all methods defined
in the Collection interface. In addition, List interface allows operations specifying
the position of the element in the collection.
Most important things to remember about a List interface – any implementation
of the List interface would maintain the insertion order. When an element A is
inserted into a List (Without specifying position) and then other element B is
inserted, A is stored before B in the List.

Ques: ArrayList with an Example?


Ans: ArrayList implements the list interface. So, ArrayList stores the element in the
insertion order (by default). Element’s can be inserted into and removed from
ArrayList based on their position.

Ques: Can ArrayList have Duplicate elements?


Ans: ArrayList can have duplicates (since List can have duplicates).
Ques: What is LinkedList? What interface does it implement? How is it different
from an ArrayList?
Ans: LinkedList extends List and Queue. Other than operations exposed by the
Queue interface, LinkedList has the operation as an ArrayList. However, the
underlying implementation of Linked List is different from that of an ArrayList.
ArrayList uses an Array kind of the structure to store element. So, inserting and
deleting from an ArrayList are explosive operations. However, search of an
ArrayList is faster than LinkedList.
LinkedList uses a linked representation. Each object holds a link to the next
element. Hence insertion and deletion are faster than ArrayList. But searching is
slower.

Ques: What is set interface?


Ans: The Set interface does not allow duplicate.

Ques: What is the Hashset?


Ans: HashSet implement set interface. So, Hashset does not allow duplicates.
However, Hashset does not support ordering. The order in which elements are
inserted is not maintained.

Ques: What is LinkedHastSet? How is different form a Hashset?


Ans: LinkedHastSet implements set interface and exposes similar operations to a
Hashset. Difference is that LinkedHastSet maintain insertion order. When we
iterate a LinkedHastSet, we would get the elements back in the order in which
they were inserted.

Ques: What is TreeSet? How is different from a HashSet?


Ans: TreeSet implements Set interface. TreeSet is similar to HashSet except that it
stores elements in Sorted Order.

Ques: What is Deque Interface?


Ans: A linear collection that supports element insertion and removal at both ends.

Ques: what is Priority Queue?


Ans: It implement Queue Interface. The elements of the priority queue are
ordered according to their natural ordering.

Ques: What is Queue Interface?


Ans: Queue interface extends Collection interface. It is typically used for the
implementation holding elements in order for some processing. It offers 2
methods, one is peak () method and other one is poll () method which get the
element at the head of the queue. The difference is that poll () method removes
the head from the queue also. Peek () would keep head of the queue unchanged.

Ques: What is Map interface? Please explain this?


Ans: Map interface doesn’t extend the Collection Interface. So, it does not inherit
any of the methods from the collection interface.
A map interface supports the Collections that uses a key value pair. A key value
pair is a set of linked data items: a key, which is unique identifier for some item of
data, and the value, which is either data or pointer to a data. Key value pairs are
used in lookup tables, hash table and configuration files. A key value pair in a map
interface is called as Entry.

Ques: What is HashMap?


Ans: HashMap implements Map interface - there by supporting key value pairs.

Ques: what is TreeMap?


Ans: TreeMap is similar to the HashMap except that it stores keys in sorted order.
It implements NavigableMap interface and SortedMap interface along with the
Map Interface.

You might also like