0% found this document useful (0 votes)
11 views

Collection Framework

Uploaded by

sundarmatsa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Collection Framework

Uploaded by

sundarmatsa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Java Collection Framework

1. What is the Java Collections Framework, and why is it essential in Java programming?

Answer: The java collection framework provides a set of classes and interfaces for working with collection
of objects. It provides fundamental data structures and algorithms for organizing and manipulating data.

2. Name some core interfaces in the Java Collections Framework.

Answer: List, Queue, Map, Set and their respective Sub interfaces.

3. What is Iterable Interface?

Answer: Iterable interface is the root interface for all collection classes.

4. What is java ArrayList?

Answer: The java ArrayList class uses a Dynamic Array for storing elements. It is like an array, but there is
no size limit. We can add or remove elements at any time. ArrayList maintains insertion order internally
and can have duplicate elements also. ArrayList is a non-Synchronized.

5. Explain the difference between a List and a Set in the Collections Framework.

Answer: A List can have duplicate elements and maintains insertion order. A set does not allow duplicate
elements and it may or may not maintain insertion order.

6. What is the purpose of the ‘Collection’ interface, and what methods does it define?

Answer: The collection interface is the root interface in the collection frameworks. It defines methods for
basic collection operations like adding, removing, and iterating over elements.

7. Name a class that implements the ‘Set’ interface in the Collections Framework.

Answer: HashSet, TreeSet, and LinkedHashSet are classes that implement the Set interface.

Syntax for creating a HashSet:

Set<Type> set = new HashSet<> ();


8. Explain the ‘Map’ interface and its key-value association.

Answer: The Map interface defines a collection that holds key-value pairs. Each key value pair is known
as entry. Each key is associated with a value. A map doesn’t allow duplicate keys, but you can have
duplicate elements.

9. What is the difference between ‘HashMap’ and ‘Hashtable’ in the Collections Framework?
Answer: HashMap is not synchronized and allows duplicate keys and values whereas Hashtable is
synchronized and does not allow duplicate keys or values.
10. Explain the ‘List’ interface and name a few classes that implement it.
Answer: The List interface extends collection interface that represents ordered collection of elements. It
allows duplicate elements and maintains insertion order. Classes that implement ArrayList, LinkedList and
Vector
11. What is the ‘Queue’ interface in the Collections Framework, and how is it different from a List?
Answer: The queue interface extends collection interface that represents a collection that ordered
elements for processing. Typically using ‘First-in first-out’ order.
12. What is the ‘Deque’ interface, and how does it differ from a Queue?
Answer: The deque is the double ended queue extends queue interface and allows elements to be added
or removed from both ends.
13. Explain the ‘Iterator’ interface and its role in traversing collections.
Answer: The iterator interface is used to iterating collection, providing methods like next() and Hashnext()
to access the elements sequentially.
Syntax:
Iterator<type> iterator = collections.iterator();
While (iterator.Hashnext()) {
Type element = iterator.next();
}
14. What is an “unmodifiable” or “immutable” collection, and why is it useful?
Answer: The immutable collection means it cannot be changed after creation and it useful for read-only
views of collections, ensuring data integrity.
Syntax:
List<type> unmodifablelist = Collections.unmodifiableList(list);
15. Explain the ‘Comparable’ interface and its role in sorting elements.
Answer: The comparable interface is used to order the objects of the user-defined class. This interface is
found in java.lang package and contains only one method named compareTo() method. It provides single
sorting sequence i.e., you can sort the elements on the basis of single data member only.
16. What is the purpose of the ‘Comparator’ interface in the Collections Framework?
Answer: The comparator interface is used to order the objects of the user-defined class. This interface is
found in java.util package and provides 2 methods i.e., compare(object obj1, object obj2) and
equals(object element). It provides multiple sorting sequence i.e., you can sort the elements on the basis
of multiple data member.
17. What is the ‘Collections’ class in Java, and what utility methods does it offer for collections?
Answer: The collection class is the utility class that provides various methods for working with collection,
such as sorting, searching, synchronization.
18. Explain the ‘hashCode’ and ‘equals’ methods in the context of using objects as keys in a Map.
Answer: The Hashcode method provides hash code for an object while equals method is used to compare
the objects for equality.
19. What is the ‘HashSet’ class in the Collections Framework, and how does it store elements?
Answer: The hashSet is the implementation of the set interface that uses hash table for storing elements.
It does not allow duplicate elements and does not maintain insertion order, allows null values. HashSet is
non synchronized.
20. Explain the ‘TreeSet’ class in the Collections Framework and its characteristics.
Answer: TreeSet is the implementation of the set interface that stores the elements in the sorted order
i.e., ascending order by default. It can contain unique values and doesn’t allow null values. TreeSet class is
non-synchronized.
21. What is the ‘LinkedList’ class in the Collections Framework, and how does it differ from an ArrayList?
Answer: LinkedList is the doubly LinkedList implementation that can be used to store the elements. It
implements the list and deque interface. It maintains insertion order and allows duplicate elements.
22. What are the differences between single and doubly linked list?
Answer: Singly linked list consists of 2 fields – data field, link to the next node. DLL consists of three fields
– previous node link, data field, next node link.
SLL uses less memory and DLL uses more memory.
23. Explain the ‘HashMap’ class in the Collections Framework, and how does it store key-value pairs?
Answer: HashMap is the implementation of the map interface that uses hash table for storing key-value
pairs. It can have unique elements. It may have one null key and multiple null values. HashMap is non-
synchronized.
24. What is the ‘Hashtable’ class, and what are its main characteristics?
Answer: Hashtable is the synchronized implementation of the map interface. It ensures thread-safety but
slower than HashMap.
25. What is the ‘Vector’ class, and how does it differ from an ArrayList?
Answer: vector is the synchronized version of the ArrayList. It ensures thread-safety but slower due to
synchronization.
26. Explain the ‘PriorityQueue’ class in the Collections Framework, and what ordering does it use?
Answer: PriorityQueue is the implementation of the Queue interface that orders elements based on
priority. The high priority element appears at front and low priority element appears at last.
27. What is the ‘ConcurrentHashMap’ class, and why is it useful for multithreaded applications?
Answer: ConcurrentHashMap is the concurrent implementation of the Map interface that allows multiple
threads to access and modify it concurrently, with high-performance and safety.
28. Explain the ‘Arrays’ class in Java and its utility methods for working with arrays.
Answer: Arrays class provides various static methods for working with arrays, including sorting, searching
and converting arrays to collections.
29. What is the purpose of the ‘CopyOnWriteArrayList’ class, and how does it work in multithreaded
environments?
Answer: CopyOnWriteArrayList is the list implementation that ensures thread-safety by providing a new
copy of a list whenever an element is modified. It is useful when reads are much more frequent than
writes.
30. Explain the ‘WeakHashMap’ class, and how does it handle weak references to keys?
Answer: WeakHashMap is the implementation of map interface which uses weak references for keys. Keys
can be collected by garbage collector if there are no strong keys are held.
31. What is the ‘EnumSet’ class in the Collections Framework, and how is it used with enums?
Answer: EnumSet is specialized set implementation for use with Enum types. It is highly efficient and takes
advantages of Enum characteristics.
32. Explain the ‘NavigableSet’ interface in the Java Collections Framework and its key features.
Answer: The NavigableSet interface extends the set interface that provides navigation methods for
traversing elements in a sorted order. It offers methods for floor, ceiling, lower and higher elements.
33. What is the ‘LinkedBlockingQueue’ class, and how does it differ from other blocking queue
implementations?
Answer: LinkedBlockingQueue is the implementation of the blocking queue interface that uses linked
node structure. It is unbounded, which means it can hold unlimited number of elements.
34. What is the ‘ConcurrentLinkedQueue’ class, and how does it handle concurrent access in a queue?
Answer: ConcurrentLinkedQueue is the concurrent implementation of the Queue interface that provides
high performance, non-blocking, and thread-safe for managing the elements in a queue
35. What is the ‘ConcurrentSkipListSet’ class, and how does it provide concurrent access to a sorted set?
Answer: ConcurrentSkipListSet is the concurrent implementation of the navigableset interface that allows
multiple threads to access and modify it concurrently while maintaining the sorted order.
36. What is the ‘EnumMap’ class, and how is it used to associate keys with enums in a Map?
Answer: EnumMap class is the specialized implementation of the Map interface. It is highly efficient and
each key is associated with specific enums.
37. Explain the ‘BlockingQueue’ interface in the context of concurrent programming.
Answer: BlockingQueue interface represents the thread safe Queue that provides blocking operations for
managing concurrent access to elements.
Answer:
38. What is the ‘ArrayDeque’ class, and how does it implement a double-ended queue (deque)?
Answer: ArrayDeque is the resizable array-based implementation of the deque interface. We can add or
remove elements from both ends and it can contain null values. ArrayDeque is faster than LinkedList and
Stack.
39. Explain the ‘LinkedHashSet’ class and its characteristics in maintaining a predictable order?
Answer: LinkedHashSet is the implementation of the set interface which maintains insertion order and it
allows null values. It combines the features from both HashMap and LinkedList.
40. What is the ‘IdentityHashMap’ class?
Answer: IdentityHashMap is the specialized map implementation that uses reference equality for keys.
41. What is the ‘TreeMap’ class in the Java Collections Framework, and how does it provide sorted key-
value mappings?
Answer: TreeMap is an implementation of the map interface that stores key-value pairs in sorted order
based on the keys. It uses red-black tree for efficient maintenance. It contains only unique elements.
42. What is the ‘Properties’ class?
Answer: The properties class is a subclass of Hashtable. It can be used to maintain list of values in which
the key is a string and the value is also a string. The properties class provides methods to get data from
properties file and store the data into properties file.
43. What differences exist between Iterator and ListIterator?
Answer:
- An Iterator can be used to traverse the set and list collections, while the ListIterator can be used
to iterate over only lists.
- An iterator can traverse the collection only in forward direction, while the ListIterator can traverse
the list in both directions.
44. What differences exist between HashMap and Hashtable?
Answer:
- HashMap is non-Synchronized, while HashTable is synchronized.
- HashMap allows one null key and multiple null values, while Hashtable doesn’t allow null values
and keys.
45. What is difference between Array and ArrayList? When will you use Array over ArrayList?
Answer:
- An Array can contain primitive Or Objects, while ArrayList can contain Objects only.
- An Array can have fixed/static size, while ArrayList is Dynamic in size.
- An Array is multi-dimensional, while ArrayList is single-dimensional.
- We can use for and for-each loop to iterate over the Array, while iterator can be used to iterate
over the ArrayLists.
46. What is difference between ArrayList and LinkedList?
Answer:
- ArrayList internally uses Dynamic Array to store elements, while LinkedList internally uses Doubly
LinkedList to store elements.
- Manipulation with ArrayList is slow because it internally uses an array, manipulation with
LinkedList is faster than ArrayList.
- ArrayList is better for storing and accessing data, while LinkedList is better for manipulating data.
47. What are the differences between Comparable and Comparator interface?
Answer:
- Comparable interface provides the single sorting sequence i.e., you can sort the elements on the
basis of single data member only such as id, name. while Comparator interface provides the
multiple sorting sequence i.e., you can sort the elements on the basis of multiple data members.
- Comparable interface provides compareTo() method to sort the elements, while Comparator
interface provides compare() method to sort the elements
- Comparable is present in java.lang package, while Comparator is present in java.util package.
48. What is the difference between HashSet and TreeSet?
Answer:
- Elements in HashSet is not ordered, while TreeSet is sorted order.
- HashSet allows null objects, while TreeSet doesn’t allow null objects.
- HashSet internally uses HashMap to store elements, while TreeSet internally uses TreeMap.
- HashSet uses equals() and Hashcode() methods for comparison, while TreeSet provides Compare()
and compareTo() methods.
49. What is difference between ArrayList and Vector?
Answer:
- ArrayList is non-Synchronized, while Vector is synchronized.
- ArrayList is faster, While Vector is slower.
- ArrayList allows multiple threads, while Vector allow single thread only.
50. What is Stack?
Answer: Stack is a linear DS that can be used to store the collection of objects. It is based on Last-in-first-
out (LIFO). It can have two important operations such as push and pop. The push operation inserts an
element into the stack and pop operation removes an element from the top of the stack.

You might also like