1

I have SQL (HSQLDB) table with VALS DOUBLE ARRAY[2000] field I use query that returns multiple rows with VALS field If i try to get array as

Array array = rs.getArray("VALS");
Double[] vals = (Double[]) array.getArray();

i get

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Double;

I can get values with double conversion from Object to String and then parsing String to Double like:

List<Double> values = new ArrayList<Double>();
for (Object o: (Object[])array.getArray()) {
    values.add(Double.parseDouble(o.toString()));
}

But it looks like heavy overhead

Is there any way to get digits from Array SQL field without String conversion or multiple single-row queries? In debugger rs.getArray() shows me a perfect JDBCArray of digital values ARRAY[0.0E0,0.0E0,0.0E0,0.0E0,0.0E0 .... ]

2 Answers 2

1

You don't need to convert to String and back. Just cast:

List<Double> values = new ArrayList<Double>(); 
for (Object o: (Object[])array.getArray()) {
    values.add((Double) o); 
}
Sign up to request clarification or add additional context in comments.

Comments

1

Try the code below:

Double[] vals = Arrays.stream(array.getArray()) .map(Double::valueOf) .toArray(Double[]::new);

1 Comment

Unresolved compilation problem: The method stream(T[]) in the type Arrays is not applicable for the arguments (Object) If i add cast to Double[] i get the same exception as in the beginning, if i cast to Object[] than i get Unresolved compilation problems: Cannot infer type argument(s) for <R> map(Function<? super T,? extends R>)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.