Common Java Containers and Their Differences
This article explains the most frequently used Java containers, clarifies the distinctions between Collection and Collections, compares List, Set, and Map, and details the implementation principles and usage differences of HashMap, HashSet, ArrayList, LinkedList, and related collection classes.
Containers
18. What Java containers exist?
Common container diagram:
19. What is the difference between Collection and Collections?
java.util.Collection is a top‑level interface for collection classes, providing generic methods for basic operations; its direct sub‑interfaces include List and Set.
Collections is a utility class that offers static methods for sorting, searching, and making collections thread‑safe.
20. What are the differences among List, Set, and Map?
21. What is the difference between HashMap and Hashtable?
HashMap removes Hashtable's contains method but adds containsValue() and containsKey().
Hashtable is synchronized while HashMap is not, making HashMap faster.
HashMap allows null keys and values; Hashtable does not.
22. How to decide between HashMap and TreeMap?
Use HashMap for fast insert, delete, and lookup; choose TreeMap when you need ordered keys for traversal.
23. Explain the implementation principle of HashMap.
HashMap is a non‑synchronized implementation of the Map interface based on a hash table. It stores entries in an array of buckets; each bucket holds a linked list (or a red‑black tree when the list exceeds eight elements) of nodes. When putting a key, its hashcode is rehashed to locate the bucket; if the bucket already contains entries, the new node is added to the head of the linked list.
24. Explain the implementation principle of HashSet.
HashSet is backed by a HashMap.
The set elements are stored as keys in the underlying HashMap.
The HashMap values are a constant placeholder (PRESENT).
25. What is the difference between ArrayList and LinkedList?
ArrayList uses a dynamic array, providing O(1) random access, while LinkedList uses a doubly‑linked list, offering O(n) access and efficient insertions/removals at both ends.
26. How to convert between an array and a List?
List → array: call list.toArray() on an ArrayList.
Array → List: use Arrays.asList(array) .
27. What is the difference between ArrayList and Vector?
Vector is synchronized; ArrayList is not. For concurrent modifications during iteration, consider CopyOnWriteArrayList .
ArrayList is generally faster because it lacks synchronization overhead.
ArrayList is more versatile; Collections provides utilities to obtain synchronized or read‑only views.
28. What is the difference between an Array and an ArrayList?
Arrays can hold primitive types and objects; ArrayList holds only objects.
Arrays have a fixed size; ArrayList can grow dynamically.
ArrayList offers many utility methods such as addAll , removeAll , and iterators.
29. What is the difference between poll() and remove() in Queue?
Both retrieve and remove the head element, but poll() returns null if the queue is empty, whereas remove() throws an exception.
30. Which collection classes are thread‑safe?
Vector – synchronized version of ArrayList.
Stack – synchronized LIFO stack.
Hashtable – synchronized map.
Enumeration – legacy iterator providing thread‑safe enumeration.
31. What is an Iterator?
An Iterator is a design pattern object that allows traversal of a collection without exposing its underlying structure, typically lightweight to create.
32. How to use an Iterator? What are its characteristics?
In Java, obtain an iterator via collection.iterator(), then repeatedly call hasNext() and next() to access elements, and optionally remove() to delete the current element. The basic Iterator moves only forward.
33. What is the difference between Iterator and ListIterator?
Iterator works for Set and List; ListIterator works only for List.
Iterator supports forward traversal only; ListIterator supports both forward and backward traversal.
ListIterator extends Iterator with additional operations such as add, replace, and obtaining previous/next indices.
(End)
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.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
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.
