0

When I run my code I get the following error:

Caused by: java.lang.ClassCastException: java.util.Collections$UnmodifiableSet cannot be cast to [Ljava.lang.Integer;

The code that causes is:

Integer[] selects = (Integer[]) tbl_analytes.getValue();

But when I do this:

Object obj = tbl_analytes.getValue();
System.out.println(obj);

I get the following output:

[1,7,15]

I don't understand why I can't convert this to an Integer[]. Any tips?

3
  • 3
    Just because something prints out like an array doesn't mean it is one. In fact, if you try to directly print an array you wont even get that nice of an output. Commented Nov 17, 2014 at 7:42
  • Casting does not do any automatic conversion (at least not for non-primitive types). Commented Nov 17, 2014 at 7:43
  • 1
    What data type is tbl_analytes ? Is it a Map ? Commented Nov 17, 2014 at 7:57

6 Answers 6

2

It seems that 'tbl_analytes' is a Set, not an array. You can not cast it, you have to convert it:

Integer[] array = set.toArray(new Integer[0]);
Sign up to request clarification or add additional context in comments.

3 Comments

it seems that what I'm getting returned doesn't have a 'toArray' function available.
have you tried gastintg the getValue? (Set) tbl_analytes.getValue().
You can try System.out.println(tbl_analytes.getClass.getName()); to find out the type of your object.
1

The Collection interface has methods to convert Collections to arrays :

Object[] toArray();

or

<T> T[] toArray(T[] a);

You can't just cast an object one one type to an unrelated type and expect it to work.

The fact that printing the Set produces an output that looks like the output you get when printing an array doesn't mean anything.

Comments

1
final ArrayList<Integer> integerList = new ArrayList<>();
final Integer[] integerArray = new Integer[integerList.size()];
integerList.toArray(integerArray);

Comments

0

You have to use the toArray(T a[])-method defined in the Collection interface, passing an empty Integer[] to the method. This solution assumes, that your List is of type List<Integer>. Otherwise, this would fail.

Comments

0
Set<Integer> values = (Set<Integer>) tbl_analytes.getValue();

The class cast exception gives the actual type. So use that or better a generalisation (List instead of ArrayList etcetera).

Comments

0

In the java language arrays are primitive. Collections are not.

Since a set is unordered it has no way to understand how to build an ordered array from a hashed set of memory locations.

When you output a set you are calling the toString method on that set.

So:

When I call

Integer[] selects = (Integer[]) tbl_analytes.getValue();

I get the following error:

Caused by: java.lang.ClassCastException: java.util.Collections$UnmodifiableSet cannot be cast to [Ljava.lang.Integer;

But when I do this:

Object obj = tbl_analytes.getValue();
System.out.println(obj);

I get the following output [1,7,15]

The reason is Integer[] is completely uncastable from Set<Integer> but the output from toString is derived from:

The java.util.Collections$UnmodifiableSet extends java.util.AbstractCollection

https://docs.oracle.com/javase/10/docs/api/java/util/AbstractCollection.html#toString()

Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).

That is the resason you are getting the exception. The solution to it is answered here in this stack overflow question:

How to convert Set<String> to String[]?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.