Java Collections and Key Programming Comparisons
This document provides a comprehensive comparison of Java collections, interfaces, classes, and key programming concepts.
1. HashMap vs. Hashtable
| Feature | HashMap | Hashtable |
|---|---|---|
| Synchronization | Non-synchronized; not thread-safe without external synchronization. | Synchronized; thread-safe by default. |
| Nulls | Allows one null key and multiple null values. | Does not allow null keys or values. |
| Version | Introduced in JDK 1.2 (new class). | Legacy class. |
| Performance | Fast. | Slower. |
| Synchronization Option | Can be made synchronized with Collections.synchronizedMap(hashMap). | Internally synchronized; cannot be unsynchronized. |
| Traversal | Uses Iterator. | Uses Enumeration and Iterator. |
| Fail-Fast Behavior | Iterator is fail-fast. | Enumerator is not fail-fast. |
| Inheritance | Inherits AbstractMap. | Inherits Dictionary. |
2. ArrayList vs. LinkedList
Both implement the List interface and maintain insertion order.
Both are non-synchronized classes.
| Feature | ArrayList | LinkedList |
|---|---|---|
| Internal Structure | Dynamic array. | Doubly linked list. |
| Best For | Adding and retrieving elements. | Removing elements. |
| Interfaces | Implements List. | Implements List and Deque. |
| Performance | Better for storing and accessing data. | Better for data manipulation. |
3. List vs. Set
Both interfaces extend the Collection interface.
| Feature | List | Set |
|---|---|---|
| Definition | Ordered sequence of elements. | Unordered collection of unique elements. |
| Duplicates | Allows duplicates. | Does not allow duplicates. |
| Order | Maintains insertion order. | Does not maintain order. |
| Access | Can access elements by index. | Cannot be accessed by index. |
| Implementations | ArrayList, LinkedList. | HashSet, LinkedHashSet, TreeSet. |
4. Enumeration vs. Iterator
| Feature | Enumeration | Iterator |
|---|---|---|
| Functionality | Only traverses; cannot modify during traversal. | Can remove elements while traversing. |
| JDK Version | Introduced in JDK 1.0. | Introduced in JDK 1.2. |
| Usage | Legacy classes: Vector, Stack, Hashtable. | Collections like ArrayList, HashSet, HashMap. |
| Methods | hasMoreElements(), nextElement(). | hasNext(), next(), remove(). |
| Nature | Fail-safe. | Fail-fast. |
| Security | Less secure. | Safer for concurrent modifications. |
| Performance | Faster, uses less memory. | Slightly slower, but thread-safe. |
Summary: Enumeration is simple and fast, suited for legacy classes. Iterator is safer and preferred for modern collections.
5. Interfaces vs. Abstract Classes
| Feature | Interface | Abstract Class |
|---|---|---|
| Implementation | Only contains method signatures. | Can have both abstract and concrete methods. |
| Inheritance | Supports multiple inheritance. | Does not support multiple inheritance. |
| Variables | Only static and final. | Can have various types of variables. |
| Methods | No constructors or static methods (until Java 8 default/static methods). | Can have constructors and static methods. |
| Usage | Defines a contract. | Defines a partially implemented template. |
| Extension | Changes require updates in all implementing classes. | Can provide default implementations. |
| Performance | Slightly slower due to method resolution. | Faster and more structured. |
6. Additional Comparisons
String vs. StringBuffer
- String: Immutable.
- StringBuffer: Mutable and synchronized.
Array vs. ArrayList
- Array: Fixed size; stores primitives or objects.
- ArrayList: Dynamic size; stores only objects.
Overloading vs. Overriding
- Overloading: Same class; same method name, different parameters (compile-time).
- Overriding: Parent-child classes; same signature (run-time).
Error vs. Exception
- Error: Serious issues not handled by programs (e.g., system crash).
- Exception: Recoverable issues handled with
try-catchorthrows.
7. Key Java Keywords & Concepts
| Concept | Description |
|---|---|
throw vs. throws | throw creates an exception manually; throws declares possible exceptions in a method signature. |
Map vs. HashMap | Map is an interface; HashMap is its implementation. |
Vector vs. ArrayList | Vector is synchronized; ArrayList is not. |
public vs. non-public class | Public classes can be accessed from other packages; non-public classes cannot. |
& vs. && | & is bitwise and evaluates both operands; && is logical and short-circuits. |