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
What package supports Reflection in Java?
The java.lang.reflect package
How does Reflection interact with Java security?
The JVM’s security manager restricts Reflection to prevent unauthorized access to private fields or methods
Privileges can be granted in specific scenarios (e.g., IDE debugging)
What is the entry point for Reflection in Java?
The java.lang.Class object:
Immutable instance created by the JVM for every object type
Provides methods to query object properties and create new objects
How can you get a Class object?
Using getClass() on an object instance:
Class<?> c = obj.getClass();
Using .class on a type:
Class<?> c = int.class;
Using Class.forName(String) with a fully qualified name:
Class<?> c = Class.forName(“java.lang.String”);
What are some key methods in the Class object?
getFields(): Public fields, including inherited ones
getDeclaredFields(): All fields, irrespective of access modifiers, excluding inherited ones
getMethods(): Public methods, including inherited ones
getDeclaredMethods(): All methods, irrespective of access modifiers, excluding inherited ones
getConstructors(): Public constructors.
getDeclaredConstructors(): All constructors, including private ones
How are objects instantiated reflectively?
Two methods are commonly used:
Class.newInstance():
Invokes no-argument constructor.
Requires the constructor to be visible.
Constructor.newInstance():
Can invoke any constructor, including private ones (if allowed)
Preferred method
What is AccessibleObject.setAccessible()?
Temporarily overrides JVM security to allow access to private members
Usage depends on the security manager and JVM policy
What are the limitations and risks of Reflection?
Performance overhead: Reflective operations are slower than direct calls
Security concerns: Reflection can access private fields and methods
Compatibility issues: Reflective code may break with JVM upgrades
Potential side effects: May render code non-portable and dysfunctional
What are common use cases for Reflection (hint: D,T,F)?
Debugging: Inspecting running programs
Testing: Forcing specific states or ensuring code coverage
Frameworks: Dynamically handling objects and classes