Essential Differences and Usage Tips for Java Collections
This article explains why Map does not extend Collection, compares HashMap with Hashtable, clarifies Comparable versus Comparator, shows how to sort object lists, distinguishes fail‑fast from fail‑safe iterators, and outlines key differences among Set, List, ArrayList, Vector, and related Java collection classes.
Why doesn't the Map interface extend the Collection interface?
Set is an unordered collection that disallows duplicate elements; List is an ordered collection that permits duplicates; Map stores key‑value pairs and can be viewed as a combination of a set of keys and a set of values. Because Map is designed specifically for key‑value pairs, it does not need to inherit from the Collection interface.
Differences between HashMap and Hashtable
Synchronization and thread safety
Support for null keys and null values
Iteration behavior
Default capacity size
Comparable vs. Comparator
Comparable is defined in java.lang and provides the compareTo(Object obj) method for ordering objects.
Comparator is defined in java.util and provides the compare(Object obj1, Object obj2) method for ordering objects.
How to sort a List of objects
For an array of objects, use Arrays.sort().
For a collection of objects, use Collections.sort().
Fail‑fast vs. fail‑safe
Fail‑fast iterators quickly report any failure; a concurrent modification (e.g., another thread modifying the collection while iterating) triggers a ConcurrentModificationException.
Fail‑safe iterators operate on a copy of the collection, allowing modifications without throwing an exception.
Iterator, ListIterator, and Enumeration differences
Enumeration (introduced in Java 1.2) is a legacy interface that uses the elements() method.
Iterator is implemented by all Java collection classes, accessed via the iterator() method, and can traverse in only one direction.
ListIterator is specific to List implementations, accessed via listIterator(), and supports bidirectional traversal.
Set vs. List
Set does not allow duplicate elements, has no index, and permits at most one null value; common implementations include HashSet, LinkedHashSet, and TreeSet.
List allows duplicate elements, maintains insertion order, provides indexed access, and permits multiple null values; common implementations include ArrayList, LinkedList, Vector, and Stack.
ArrayList vs. Vector
Vector was introduced in the first version of Java and is synchronized (thread‑safe).
ArrayList was added in Java 1.2 as part of the Collections Framework and is unsynchronized (not thread‑safe by default).
Classes that implement the List interface
ArrayList LinkedList VectorClasses that implement the Set interface
HashSet LinkedHashSet TreeSetHow to ensure a collection is thread‑safe
Classes such as Vector, Hashtable, Properties, and Stack are synchronized and thus thread‑safe.
Use Collections.synchronizedList(list) to obtain a thread‑safe list.
Use Collections.synchronizedSet(set) to obtain a thread‑safe set.
Can null be added to TreeSet or HashSet?
Both HashSet and TreeSet allow a single null element.
Why can't Collection extend Cloneable and Serializable?
Only List and Set directly extend the Collection interface. SortedMap extends the Map interface instead.
Importance of hashCode() and equals()
Both methods are defined in java.lang.Object.
If equals() returns true for two objects, their hashCode() values must be identical.
Array vs. ArrayList
An array has a fixed size and holds a homogeneous collection of elements.
An ArrayList can hold both homogeneous and heterogeneous elements and can grow dynamically.
What is the Properties class?
Propertiesis a subclass of Hashtable used to maintain a list of key‑value pairs where both keys and values are strings.
How to convert a String to an ArrayList
Use the Arrays.asList(string.split(...)) method or create a new ArrayList from the resulting array; the toArray() method converts an ArrayList back to an array.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
