Java Collections Framework
Flashcard 1
Q: What is the Java Collections Framework?
A: The Java Collections Framework (JCF) is a set of classes and interfaces that implement
commonly reusable collection data structures, such as lists, sets, and maps, allowing for
organized data storage and manipulation.
Flashcard 2
Q: How do you create an ArrayList and add elements in Java?
A:
java
Copy code
import java.util.ArrayList;
ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
Flashcard 3
Q: What method would you use to remove an element from a List in Java?
A: remove(int index) or remove(Object o).
Example:
java
Copy code
list.remove(0); // Removes the first element
list.remove("Banana"); // Removes the specified object
Flashcard 4
Q: What is a Set in Java, and which classes implement it?
A: A Set is a collection that cannot contain duplicate elements. Common implementations
include HashSet, LinkedHashSet, and TreeSet.
Flashcard 5
Q: How does a HashSet differ from a LinkedHashSet?
A: HashSet makes no guarantee of the iteration order, while LinkedHashSet maintains
insertion order.
Flashcard 6
Q: Write a code sample to create a HashSet and add elements to it.
A:
java
Copy code
Java Collections Framework
import java.util.HashSet;
HashSet<String> set = new HashSet<>();
set.add("Red");
set.add("Blue");
set.add("Green");
Flashcard 7
Q: Describe the Map interface in Java.
A: A Map represents a collection of key-value pairs, where each key maps to exactly one
value. Implementations include HashMap, LinkedHashMap, and TreeMap.
Flashcard 8
Q: How do you retrieve a value from a HashMap given a key?
A: Use the get(Object key) method.
Example:
java
Copy code
import java.util.HashMap;
HashMap<Integer, String> map = new HashMap<>();
map.put(1, "One");
String value = map.get(1); // Retrieves "One"
Flashcard 9
Q: How does TreeMap order its elements?
A: TreeMap orders its elements based on the natural ordering of keys or by a specified
Comparator.
Flashcard 10
Q: What is the purpose of the Collections utility class in Java?
A: The Collections class provides static methods to manipulate collections, like sorting,
searching, and synchronizing them.