ECM2414 Collection Flashcards
What is a collection in Java?
A collection is an object that groups multiple elements into a single unit, such as a poker hand, a mail folder, or a telephone directory.
What is the difference between Collection and Map in Java?
Collection: A group of objects known as elements.
Map: A group of objects where each object maps keys to values.
What does the Set interface ensure?
A Set contains no duplicate elements, and two sets are equal if they have the same elements, regardless of their internal order.
What are the main concrete implementations of the Set interface?
HashSet: Fast, no order guarantee.
TreeSet: Ordered, uses a red-black tree.
LinkedHashSet: Maintains insertion order.
How does the List interface differ from Set?
A List is an ordered collection of elements that may contain duplicates, and two lists are equal if they have the same elements in the same order.
What are the main concrete implementations of the List interface?
ArrayList: Uses a dynamically resizing array.
LinkedList: Uses a doubly linked list.
What is a Queue in Java?
A Queue is a collection for processing elements, typically in first-in-first-out (FIFO) order.
What methods differentiate Queue operations?
Add vs. Offer: add throws an exception if capacity is exceeded, while offer returns false.
Remove vs. Poll: remove throws an exception if empty, while poll returns null.
Element vs. Peek: element throws an exception if empty, while peek returns null.
What is a Deque?
A Deque (double-ended queue) allows insertions and deletions at both ends.
What does the Map interface represent?
A Map maps keys to values, with each key mapping to at most one value. It uses two generic type arguments, <K, V>.
What are the main concrete implementations of the Map interface?
HashMap: Unordered, fast.
TreeMap: Ordered by keys.
LinkedHashMap: Maintains insertion order.
What is the purpose of the Iterator interface?
It provides methods to traverse and optionally remove elements from a collection, ensuring safe iteration.
What is a ListIterator, and how does it extend Iterator?
A ListIterator allows bidirectional traversal of a list and additional methods like set() and add() for modifying the list.
How does the Java Collections Framework enhance programming?
It provides reusable and efficient data structures, supports generics, and eliminates the fixed size and limited functionality of arrays.
What does the Collection interface provide?
It defines fundamental operations like add(), remove(), size(), clear(), and bulk operations like addAll() and retainAll().