Week 9 Flashcards
What is the Java Collections Framework?
A framework in Java for storing and manipulating groups of objects
Provides interfaces, implementations, and algorithms for common data structures and collections
What are the core interfaces in the Collections Framework?
Collection<E>: A group of objects (elements)</E>
Map<K, V>: A mapping of keys to values
What is the Collection<E> interface?</E>
Methods include:
Size and emptiness checks: size(), isEmpty()
Membership operations: contains(Object), add(E), remove(Object)
Bulk operations: addAll(Collection), removeAll(Collection), retainAll(Collection)
Array conversions: toArray()
What are the key characteristics of a Set<E>?</E>
No duplicate elements
Common implementations:
HashSet<E>: Unordered, fast operations</E>
TreeSet<E>: Elements are sorted, slower than HashSet</E>
LinkedHashSet<E>: Maintains insertion order</E>
What are the characteristics of a List<E>?</E>
Ordered collection, allows duplicate elements
Methods for positional access: get(int index), add(int index, E), remove(int index)
Iteration support via ListIterator
What are the main implementations of List<E>?</E>
ArrayList<E>: Backed by a dynamically resizing array, fast random access</E>
LinkedList<E>: Doubly linked list, better for frequent insertions/removals</E>
What is a Queue<E>?</E>
Collection for processing elements, typically in first-in-first-out (FIFO) order
Methods:
Add elements: add(E), offer(E)
Access head: peek(), poll(), remove()
What is a Deque<E>?</E>
Double-ended queue, allows insertions and deletions at both ends
Examples: ArrayDeque, LinkedList (as a Deque)
What is a Map<K, V>?
Maps keys to values; each key maps to at most one value
Methods include:
Basic operations: put(K, V), get(Object), remove(Object)
Bulk operations: putAll(Map), clear()
Views: keySet(), values(), entrySet()
What are the main implementations of Map<K, V>?
HashMap<K, V>: Unordered, fast operations
TreeMap<K, V>: Keys sorted in natural or custom order
LinkedHashMap<K, V>: Maintains insertion order
What is an Iterator<E>?</E>
Used to traverse a collection.
Methods:
hasNext(): Checks if the next element exists
next(): Retrieves the next element
remove(): Removes the last element returned by next()
What is a ListIterator<E>?</E>
Extends Iterator with bi-directional traversal:
hasPrevious(), previous().
add(E), set(E).
What are the advantages of using the Java Collections Framework?
Simplifies programming by providing ready-to-use data structures
Ensures type safety with generics
Improves performance with optimized implementations
What should you know about the order and uniqueness in collections?
Set: No duplicates, unordered or ordered based on implementation
List: Allows duplicates, ordered
Queue: FIFO or based on other policies, allows duplicates
Map: No duplicate keys, values can be duplicated
What is Java Reflection?
The ability of a class or object to examine and manipulate itself
Enables accessing constructors, methods, and fields at runtime
Allows modifying private fields, invoking methods, and constructing new objects dynamically