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

Java Interview Collections

The document provides an overview of the Java Collections Framework, detailing key concepts such as the differences between List and Set, and how HashMap operates internally. It also explains various collection types like ArrayList, LinkedList, ConcurrentHashMap, HashSet, and TreeSet, along with their characteristics and use cases. Additionally, it covers the Iterator interface, sorting mechanisms with Comparable and Comparator, and methods for creating read-only collections.

Uploaded by

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

Java Interview Collections

The document provides an overview of the Java Collections Framework, detailing key concepts such as the differences between List and Set, and how HashMap operates internally. It also explains various collection types like ArrayList, LinkedList, ConcurrentHashMap, HashSet, and TreeSet, along with their characteristics and use cases. Additionally, it covers the Iterator interface, sorting mechanisms with Comparable and Comparator, and methods for creating read-only collections.

Uploaded by

gefehif698
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Interview Questions

Java Collections Framework

Q1. What is the Java Collections Framework?


It is an architecture to store and manipulate a group of objects. It provides interfaces
(like List, Set, Queue, Map) and classes (like ArrayList, HashSet, HashMap) along with
algorithms (like sorting and searching) to work with data structures.

Q2. What is the difference between List and Set?


A List is an ordered collection that allows duplicate elements and maintains the insertion
order. A Set is an unordered collection that does not allow duplicate elements.

Q3. How does HashMap work internally in Java?


HashMap works on the principle of hashing. It stores key-value pairs in an array of
nodes (buckets). When 'put' is called, it calculates the hashcode of the key to find the
bucket index. If collisions occur, elements are stored in a linked list (or balanced tree if
size > 8) at that bucket.

Q4. What is the difference between ArrayList and LinkedList?


ArrayList uses a dynamic array to store elements, providing fast O(1) random access
but slower insertions/deletions. LinkedList uses a doubly linked list, making random
access O(n) but insertions/deletions faster since no array shifting is required.
Q5. What is ConcurrentHashMap?
ConcurrentHashMap is a thread-safe implementation of Map introduced in Java 1.5.
Instead of locking the entire map, it locks only a segment of the map (or individual
buckets in Java 8+), allowing multiple threads to read and write concurrently without
blocking each other.

Q6. What is the Iterator interface?


Iterator is an interface used to traverse elements of a collection sequentially. It provides
methods like hasNext(), next(), and remove(), replacing the older Enumeration interface
and adding the ability to safely remove elements during traversal.

Q7. Difference between HashSet and TreeSet?


HashSet stores elements using a hash table, providing O(1) operations but no
guaranteed order. TreeSet stores elements using a Red-Black tree, providing O(log n)
operations but keeps elements in a sorted (natural or custom) order.

Q8. What is the difference between Comparable and Comparator?


Comparable provides a single natural sorting sequence for objects by implementing the
compareTo() method in the class itself. Comparator provides multiple, custom sorting
sequences by implementing the compare() method in a separate class.

Q9. How can you make a Collection read-only?


You can make a collection read-only using the [Link]()
methods (like unmodifiableList, unmodifiableSet, unmodifiableMap). Any attempt to
modify the returned collection throws an UnsupportedOperationException.
Q10. What is the difference between poll() and remove() in Queue?
Both methods remove and return the head of the queue. However, if the queue is empty,
poll() returns null, whereas remove() throws a NoSuchElementException.

Generated Document • Java Collections Framework

You might also like