Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ClassCastException when casting object Array to Long array

Tags:

java

I get this exception when i try to cast an Object array to a Long array.

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Long;

My keys in my hotelRooms map are Long, why it is not possible to cast. Does someone know how to solve this.

public class ObjectArrayToLongArrayTest {

private Map<Long, String[]> hotelRooms;

public static void main(String[] args) {

    ObjectArrayToLongArrayTest objectArrayToLongArrayTest =
        new ObjectArrayToLongArrayTest();
    objectArrayToLongArrayTest.start();
    objectArrayToLongArrayTest.findByCriteria(null);

}

private void start() {
    hotelRooms = new HashMap<Long, String[]>();
    // TODO insert here some test data.

    hotelRooms.put(new Long(1), new String[] {
            "best resort", "rotterdam", "2", "y", "129", "12-12-2008",
            "11111111"
    });

    hotelRooms.put(new Long(2), new String[] {
            "hilton", "amsterdam", "4", "n", "350", "12-12-2009", "2222222"
    });

    hotelRooms.put(new Long(3), new String[] {
            "golden tulip", "amsterdam", "2", "n", "120", "12-09-2009",
            null
    });
}

public long[] findByCriteria(String[] criteria) {

    Long[] returnValues;

    System.out.println("key of the hotelRoom Map" + hotelRooms.keySet());
    if (criteria == null) {
        returnValues = (Long[]) hotelRooms.keySet().toArray();
    }

    return null;
}
}   
like image 500
loudiyimo Avatar asked Jan 06 '10 22:01

loudiyimo


People also ask

What causes ClassCastException?

ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another. It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not an instance.

How to convert object array to int array in Java 8?

Using Java 8 Convert the specified object array to a sequential Stream. Use Stream. map() to convert every object in the stream to their integer representation. Use the toArray() method to accumulate the stream elements into a new integer array.

Can you type cast an array?

You can't cast an Object array to an Integer array. You have to loop through all elements of a and cast each one individually.


2 Answers

change

returnValues = (Long[]) hotelRooms.keySet().toArray();

to

returnValues = hotelRooms.keySet().toArray(new Long[hotelRooms.size()]);

and let me know if it works :-)

like image 182
moritz Avatar answered Oct 16 '22 06:10

moritz


It's because Object[] Set.toArray() returns an object array. You can't downcast the array to a more specific type. Use <T> T[]Set.toArray(T[] a) instead. If the generic type method didn't exist you'd have to loop through each of the Objects in the return object array and cast each individually into a new Long array.

like image 38
Jeremy Raymond Avatar answered Oct 16 '22 06:10

Jeremy Raymond