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

FeatureHashMapHashtable
SynchronizationNon-synchronized; not thread-safe without external synchronization.Synchronized; thread-safe by default.
NullsAllows one null key and multiple null values.Does not allow null keys or values.
VersionIntroduced in JDK 1.2 (new class).Legacy class.
PerformanceFast.Slower.
Synchronization OptionCan be made synchronized with Collections.synchronizedMap(hashMap).Internally synchronized; cannot be unsynchronized.
TraversalUses Iterator.Uses Enumeration and Iterator.
Fail-Fast BehaviorIterator is fail-fast.Enumerator is not fail-fast.
InheritanceInherits AbstractMap.Inherits Dictionary.

2. ArrayList vs. LinkedList

Both implement the List interface and maintain insertion order.
Both are non-synchronized classes.

FeatureArrayListLinkedList
Internal StructureDynamic array.Doubly linked list.
Best ForAdding and retrieving elements.Removing elements.
InterfacesImplements List.Implements List and Deque.
PerformanceBetter for storing and accessing data.Better for data manipulation.

3. List vs. Set

Both interfaces extend the Collection interface.

FeatureListSet
DefinitionOrdered sequence of elements.Unordered collection of unique elements.
DuplicatesAllows duplicates.Does not allow duplicates.
OrderMaintains insertion order.Does not maintain order.
AccessCan access elements by index.Cannot be accessed by index.
ImplementationsArrayList, LinkedList.HashSet, LinkedHashSet, TreeSet.

4. Enumeration vs. Iterator

FeatureEnumerationIterator
FunctionalityOnly traverses; cannot modify during traversal.Can remove elements while traversing.
JDK VersionIntroduced in JDK 1.0.Introduced in JDK 1.2.
UsageLegacy classes: Vector, Stack, Hashtable.Collections like ArrayList, HashSet, HashMap.
MethodshasMoreElements(), nextElement().hasNext(), next(), remove().
NatureFail-safe.Fail-fast.
SecurityLess secure.Safer for concurrent modifications.
PerformanceFaster, 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

FeatureInterfaceAbstract Class
ImplementationOnly contains method signatures.Can have both abstract and concrete methods.
InheritanceSupports multiple inheritance.Does not support multiple inheritance.
VariablesOnly static and final.Can have various types of variables.
MethodsNo constructors or static methods (until Java 8 default/static methods).Can have constructors and static methods.
UsageDefines a contract.Defines a partially implemented template.
ExtensionChanges require updates in all implementing classes.Can provide default implementations.
PerformanceSlightly 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-catch or throws.

7. Key Java Keywords & Concepts

ConceptDescription
throw vs. throwsthrow creates an exception manually; throws declares possible exceptions in a method signature.
Map vs. HashMapMap is an interface; HashMap is its implementation.
Vector vs. ArrayListVector is synchronized; ArrayList is not.
public vs. non-public classPublic classes can be accessed from other packages; non-public classes cannot.
& vs. &&& is bitwise and evaluates both operands; && is logical and short-circuits.