Week 7 Flashcards
What is a data structure?
A data structure is a way of organizing, managing, and storing data to enable efficient access and modification.
How are data structures classified? Data structures are classified into:
Primitive (e.g., int, char)
Non-primitive (e.g., arrays, linked lists)
Linear (e.g., arrays, stacks, queues)
Non-linear (e.g., trees, graphs)
What is the difference between linear and non-linear data structures?
Linear data structures store data in a sequential manner (e.g., arrays, lists).
Non-linear data structures organize data hierarchically or with complex relationships (e.g., trees, graphs).
What is the Java Collections Framework?
A set of classes and interfaces in Java that provide data structures and algorithms for storing and manipulating groups of objects efficiently.
What is the root interface of the Java Collections Framework?
The Collection interface is the root interface for working with groups of objects.
Name two subinterfaces of the Collection interface.
List
Set
How does the Collection interface differ from the Map interface?
Collection stores individual elements.
Map stores key-value pairs.
What are generics in Java?
Generics allow for type-safe data structures and methods by letting you specify the type of objects a class or method can work with.
Why are generics useful in Java?
They provide type safety, reduce casting, and enable code reusability.
How do you define a generic class?
class GenericClass<T> {
private T value;
public T getValue() {
return value;
}
public void setValue(T value) {
this.value = value;
}
}</T>
What is type erasure in generics?
Type erasure is the process by which the generic type information is removed at runtime, and the generic type is replaced by Object or bound types during compilation.
What is the Set interface in Java?
The Set interface is a collection that cannot contain duplicate elements.
What is the difference between Set and List?
Set does not allow duplicates, and there is no guarantee of order.
List allows duplicates and maintains insertion order.
Name two classes that implement the Set interface.
HashSet
TreeSet
How does Set ensure uniqueness of elements?
By using either hashCode() and equals() (in HashSet) or by comparing elements using their natural ordering or a comparator (in TreeSet).
What is a HashSet in Java?
A HashSet is an implementation of the Set interface that uses a hash table for storing elements, ensuring constant-time performance for basic operations.
How does HashSet determine the uniqueness of elements?
It uses the hashCode() and equals() methods of objects to ensure uniqueness.
What is a TreeSet in Java?
A TreeSet is a sorted Set implementation that stores elements in a binary search tree (usually a Red-Black tree).
How is a TreeSet different from a HashSet?
A TreeSet maintains the elements in sorted order, whereas HashSet does not guarantee any order.
Which sorting order does a TreeSet maintain?
The natural ordering of elements, or a custom ordering defined by a Comparator.
What is the Queue interface in Java?
A Queue is a collection used to hold elements prior to processing, typically following FIFO (First-In-First-Out) order.
How does a Queue differ from a Stack?
Queue: Follows FIFO (First-In-First-Out).
Stack: Follows LIFO (Last-In-First-Out).
What are two classes that implement the Queue interface?
LinkedList
PriorityQueue
What is a PriorityQueue in Java?
A PriorityQueue is a type of queue where elements are ordered based on their priority rather than the insertion order.
How does a PriorityQueue determine priority?
By using the natural ordering of elements or a comparator provided at the queue’s creation.
Does a PriorityQueue maintain the insertion order?
No, it orders elements based on their priority.
What is the Map interface in Java?
The Map interface represents a collection of key-value pairs where each key maps to one value.
How does a Map differ from a Set?
Map holds key-value pairs.
Set holds individual elements without duplicates.
What are two commonly used classes that implement the Map interface?
HashMap
TreeMap
What is a HashMap in Java?
A HashMap is an implementation of the Map interface that stores key-value pairs in a hash table, providing constant-time performance for basic operations.