1. What are Java Collections?
Java Collections are a framework of classes and interfaces in Java that are used to store and manipulate groups of objects. The Java Collections framework provides several pre-built data structures, algorithms, and utility classes for working with collections.
2. What are the differences between ArrayList and LinkedList?
ArrayList is a resizable array implementation of the List interface, while LinkedList is a linked list implementation of the List interface. ArrayList provides constant-time access to elements based on an index, while LinkedList provides constant-time insertion and deletion of elements.
3. What is the difference between a Set and a List?
A Set is an unordered collection of unique elements, while a List is an ordered collection of elements that allows duplicates. The Set interface does not provide any methods for accessing elements by their position, while the List interface provides methods for accessing elements by their index.
4. What is the difference between a HashSet and a TreeSet?
HashSet is an implementation of the Set interface that uses a hash table to store elements, while TreeSet is an implementation of the Set interface that uses a binary search tree to store elements. HashSet does not guarantee the order of elements, while TreeSet guarantees that the elements are sorted in ascending order.
5. What is the difference between a HashMap and a TreeMap?
HashMap is an implementation of the Map interface that uses a hash table to store key-value pairs, while TreeMap is an implementation of the Map interface that uses a binary search tree to store key-value pairs. HashMap does not guarantee the order of elements, while TreeMap guarantees that the elements are sorted in ascending order based on their keys.
6. What is the difference between fail-fast and fail-safe iterators?
Fail-fast iterators are iterators that throw a ConcurrentModificationException if the collection is modified while the iterator is iterating over it. Fail-safe iterators are iterators that make a copy of the collection before iterating over it, so that modifications made to the collection during iteration do not affect the iteration.
7. What is a ConcurrentHashMap?
ConcurrentHashMap is a thread-safe implementation of the Map interface in Java. It provides concurrent access to its elements and uses partitioning to lock only a part of the map during modification, which allows multiple threads to modify the map concurrently.
8. What is the difference between a Vector and an ArrayList?
Vector is a synchronized implementation of the List interface, while ArrayList is an unsynchronized implementation of the List interface. Vector is thread-safe but slower than ArrayList, while ArrayList is not thread-safe but faster than Vector.
9. What is a PriorityQueue?
PriorityQueue is an implementation of the Queue interface in Java that provides elements in a priority order. Elements are ordered based on their natural order or based on a Comparator provided at the time of creation.
10. What is the difference between an Iterator and a ListIterator?
Iterator is an interface that provides methods for iterating over a collection and removing elements from the collection during iteration. ListIterator is an interface that extends the Iterator interface and provides additional methods for iterating over a List in both forward and backward directions.
11. What is the difference between a Collection and a Collections class?
Collection is an interface in Java that represents a group of objects. The Collections class is a utility class in Java that provides several static methods for working with collections, such as sorting, searching, and copying.
12. What is the difference between a WeakHashMap and a HashMap?
WeakHashMap is an implementation of the Map interface in Java that uses weak references to keys. If the key is no longer referenced elsewhere, it is automatically removed from the map by the garbage collector. HashMap uses strong references to keys, which prevents keys from being garbage collected.
13. What is a Deque?
Deque is an interface in Java that represents a double-ended queue, which allows insertion and removal of elements from both ends of the queue. Deque stands for "Double Ended Queue." It extends the Queue interface and provides methods for adding and removing elements from both ends of the queue.
14. What is the difference between a Stack and a Queue?
Stack is a data structure that follows the Last In First Out (LIFO) principle, while Queue is a data structure that follows the First In First Out (FIFO) principle. In a stack, elements are inserted and removed from the top of the stack, while in a queue, elements are inserted at the rear and removed from the front.
15. What is the difference between a HashTable and a HashMap?
Hashtable is a synchronized implementation of the Map interface in Java, while HashMap is an unsynchronized implementation of the Map interface. Hashtable is thread-safe but slower than HashMap, while HashMap is not thread-safe but faster than Hashtable. Hashtable does not allow null keys or values, while HashMap allows null keys and values.
16. What is the difference between Enumeration and Iterator?
Enumeration is an interface in Java that provides methods for iterating over a collection. It is a legacy interface that was introduced in Java 1.0 and has been replaced by the Iterator interface. Iterator is an interface that provides more functionality than Enumeration, such as the ability to remove elements during iteration.
17. What is the difference between Arrays and ArrayList?
Arrays are a fixed-size collection of elements of the same type, while ArrayList is a dynamic collection of elements of the same type that can grow or shrink in size. Arrays can store primitives and objects, while ArrayList can only store objects. Arrays can be accessed using the [] operator, while ArrayList requires methods like get() and set() to access elements.
18. What is the difference between a TreeMap and a SortedMap?
TreeMap is a concrete implementation of the SortedMap interface in Java, while SortedMap is an interface that extends the Map interface and provides methods for maintaining the order of elements in the map. TreeMap stores key-value pairs in ascending order based on the keys, while SortedMap does not specify the order in which the elements are stored.
19. What is the difference between a Collection and an Array?
A Collection is an interface in Java that represents a group of objects, while an Array is a fixed-size collection of elements of the same type. Collections can grow or shrink in size, while arrays have a fixed size. Collections can only store objects, while arrays can store both primitives and objects.
20. What is the difference between a HashMap and a ConcurrentHashMap?
HashMap is an unsynchronized implementation of the Map interface, while ConcurrentHashMap is a synchronized implementation of the Map interface that provides better performance in concurrent environments. ConcurrentHashMap uses a partitioned approach to locking, which allows multiple threads to modify different parts of the map concurrently, while HashMap locks the entire map during modification.