Question


I am getting this error java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.lang.Comparable

Here is the code in question.

final String[] methods = parseRule.getMethods();
// add to the param.
methodsToInsert.add(methods);  // <-- error from here

//Where
public String[] getMethods() {
    return new String[]{new String(parseMethod), new String(unParseMethod)};
}

//and param
Queue<String[]> methodsToInsert

//and at some point the methodsToInsert Object is
new PriorityQueue<String[]>();

I'm not fully sure why? Any suggestions


Thanks

Wow thanks guys, I think in a previous refactoring queue was needed, but as it has evolved it is no longer required, thank you for your information and how to fix with comparators. Since PriorityQueue is no longer needed, I'll return to a List<>.

Was it helpful?

Solution

A PriorityQueue must order its elements somehow, either accepting a Comparator in its constructor, or relying on its elements being Comparable.

From PriorityQueue javadocs:

The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used. A priority queue does not permit null elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException).

You haven't supplied a Comparator, so it expects all elements added to be Comparable, but arrays aren't Comparable.

Supply your own Comparator<String[]> using the PriorityQueue constructor that takes a Comparator.

OTHER TIPS

The documentation of PriorityQueue makes it clear:

The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used. A priority queue does not permit null elements. A priority queue relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException).

You should pass a Comparator<String[]> instance to the appropriate constructor of PriorityQueue that will be used for comparison, as String[] can't be compared on natural ordering.

The PriorityQueue contains elements that are "ordered according to their natural ordering, or by a Comparator provided at queue construction time." Although Sting objects have a natural ordering, arrays of string do not. You will need to provide a Comparator object at the time you create the queue object which can say that one array of String is greater or less than another array of String.

You can't add a String[] to a PriorityQueue because a String[] is not Comparable. The solution depends on what you really need to accomplish.

Queue the Strings

Use Queue's addAll() method.

Queue the Strings and Retain Grouping

Queue your String[] objects in a Queue.

Queue the String and Retain Priority

Use PriorityQueue's addAll() method.

Queue the Strings, Retain Grouping, and Retain Priority

Wrap your String[] in a new class. Apply the Comparable interface. Implement the compareTo() and equals() methods. Queue your custom objects in a PriorityQueue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top