java.lang.ClassCastException: class java.util.LinkedList cannot be cast to class java.util.PriorityQueue
Why you’re seeing it
A cast only works when the actual object already is an instance of the requested type (or one of its subclasses). Although LinkedList and PriorityQueue both implement the Queue interface, they are two independent concrete classes. Trying to do this:
java
LinkedList<Integer> list = new LinkedList<>();
PriorityQueue<Integer> pq = (PriorityQueue<Integer>) list; // 💥 boom
will always fail at runtime, because a LinkedList object is not a PriorityQueue object.
How to “convert” one into the other
Create a new queue of the desired type and add the elements from the old one. There are two typical ways:
Constructor that accepts a Collection
Default constructor + addAll
Example – LinkedList ➜ PriorityQueue
java
LinkedList<Integer> list = new LinkedList<>();
list.addAll(Arrays.asList(5, 1, 9, 3));
// Option A: convenient constructor
PriorityQueue<Integer> pq1 = new PriorityQueue<>(list);
// Option B: manual copy
PriorityQueue<Integer> pq2 = new PriorityQueue<>();
pq2.addAll(list);
Example – PriorityQueue ➜ LinkedList (the other direction)
java
PriorityQueue<Integer> pq = new PriorityQueue<>();
pq.addAll(Arrays.asList(5, 1, 9, 3));
// Option A
LinkedList<Integer> list1 = new LinkedList<>(pq);
// Option B
LinkedList<Integer> list2 = new LinkedList<>();
list2.addAll(pq);
No conversion needed? Use the common interface
If you only care about queue operations (offer, poll, peek, …), program against the Queue<E> interface and forget about the underlying implementation:
java
Queue<Integer> q = new LinkedList<>(); // or new PriorityQueue<>();
q.offer(3);
q.offer(1);
q.poll();
This way you can swap implementations without changing the rest of your code.
Key points to remember
• A ClassCastException means “this object isn’t actually that type.”
• LinkedList and PriorityQueue share an interface but are not interchangeable via casting.
• To “convert,” build a new collection and copy the elements (constructor or addAll).
because Iterable and Collection are two different interfaces:
• Iterable<E> → minimal contract: lets you loop (for-each, iterator())
• Collection<E> → extendsIterable<E> and adds size(), add(), remove(), …
A method that is declared to receive a Collection<? extends Edge> therefore expects an object that really is a Collection (List, Set, Queue, …) so it can rely on those extra operations. Merely being Iterable is not enough.
How to fix it
If the object is already a Collection, just re-declare or cast safely
java
Iterable<Edge> edgesIter = getEdges(); // but you actually know it’s, say, a List
if (edgesIter instanceof Collection) {
someMethod((Collection<? extends Edge>) edgesIter); // safe cast
}
Even better: declare it as the right type from the start:
java
Collection<Edge> edges = getEdges(); // if the API really returns a Collection
someMethod(edges);
Wrap / copy the iterable into a real Collection
If you genuinely have only an Iterable, build a new list (or set) from it:
If you control the method that currently demands a Collection, maybe it really only needs to iterate, not to modify or query .size(). Then change the signature:
java
void someMethod(Iterable<? extends Edge> edges) {
for (Edge e : edges) { ... }
}
That removes the need for callers to wrap anything.
Use List.copyOf / Set.copyOf (Java 10+) for an unmodifiable view
• Iterable ⊂ Collection, but the compiler will not up-cast automatically.
• Convert (new ArrayList<>(…), streams), safely cast, or change the API.
• Prefer the narrowest type your method actually needs (Iterable if only reading, Collection if you require collection operations).
Once you supply an actual Collection<? extends Edge>, the error disappears ✅