Java Collection Framework Q&A
Q1. Hierarchy of the Collection Framework in Java
Java Collection Framework has the following top-level interfaces:
1. Collection
- List (ArrayList, LinkedList, Vector, Stack)
- Set (HashSet, LinkedHashSet, TreeSet)
- Queue (PriorityQueue, ArrayDeque)
2. Map (Not part of Collection)
- HashMap, LinkedHashMap, TreeMap, Hashtable
Example:
List<String> list = new ArrayList<>();
Set<Integer> set = new HashSet<>();
Map<String, String> map = new HashMap<>();
Q2. Difference between Collection Interface and Collections Class
- Collection (java.util.Collection) is an interface representing a group of objects.
- Collections (java.util.Collections) is a utility class with static methods for collection operations.
Example:
Collection<String> c = new ArrayList<>();
Collections.sort(list);
Q3. ArrayList vs LinkedList vs Vector
- ArrayList: Fast for random access, not synchronized.
- LinkedList: Fast for insert/delete, uses nodes.
Java Collection Framework Q&A
- Vector: Synchronized version of ArrayList.
Use:
- ArrayList for read-heavy lists.
- LinkedList for frequent insert/delete.
- Vector for thread-safe operations.
Q4. List vs Set Interface
- List: Ordered, allows duplicates. Examples: ArrayList, LinkedList.
- Set: Unordered, no duplicates. Examples: HashSet, TreeSet.
List<String> list = new ArrayList<>();
Set<String> set = new HashSet<>();
Q5. Stack Class in Java
Stack extends Vector and follows LIFO.
Example:
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.pop();
Program:
Stack<Integer> s = new Stack<>();
s.push(10); s.push(20);
System.out.println(s.pop()); // 20
Q6. HashMap vs LinkedHashMap vs TreeMap vs Hashtable
Java Collection Framework Q&A
- HashMap: No order, not synchronized.
- LinkedHashMap: Maintains insertion order.
- TreeMap: Sorted by keys.
- Hashtable: Thread-safe, no null keys/values.
Use:
- HashMap for fast access.
- TreeMap for sorted keys.
- Hashtable when thread-safe is needed.
Q7. Map Interface vs Collection Interface
- Map stores key-value pairs, does not extend Collection.
- Collection is for groups of elements (List, Set, Queue).
Map<String, Integer> map = new HashMap<>();
Collection<String> list = new ArrayList<>();
Q8. Comparable Interface and Custom Sort
Comparable allows objects to be sorted using compareTo().
class Student implements Comparable<Student> {
int marks;
public int compareTo(Student s) {
return this.marks - s.marks;
}
Java Collection Framework Q&A
List<Student> list = new ArrayList<>();
Collections.sort(list);
Q9. Hashtable vs HashMap
- HashMap: Not synchronized, allows one null key, many null values.
- Hashtable: Synchronized, no null keys/values.
HashMap<String, String> map = new HashMap<>();
Hashtable<String, String> table = new Hashtable<>();
Q10. Properties Class Example
Used to read/write config from .properties file.
Example:
Properties prop = new Properties();
prop.load(new FileInputStream("config.properties"));
String value = prop.getProperty("key");
File: config.properties
username=admin
password=123