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?

Eran
  • 387,369
  • 54
  • 702
  • 768

6 Answers6

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.

Eran
  • 387,369
  • 54
  • 702
  • 768
1
final ArrayList<Integer> integerList = new ArrayList<>();
final Integer[] integerArray = new Integer[integerList.size()];
integerList.toArray(integerArray);
Smutje
  • 17,733
  • 4
  • 24
  • 41
1

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]);
Ton Bosma
  • 171
  • 1
  • 6
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.

TobiasMende
  • 741
  • 3
  • 8
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).

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
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[]?

AnthonyJClink
  • 1,008
  • 2
  • 11
  • 32