cast List<Object> to List<Map<String, Object>>

11,491

Solution 1

You can just drop generic parameter by double-casting:

@SuppressWarnings("‌​unchecked")
List<Map<String, Object>> rxData = 
    (List<Map<String, Object>>) (List<?>) dataList;

What is going here is that you force compiler to not to check generic type by omitting it with first cast and then do unchecked cast to List<Map<String, Object>>. This is possible because java generics is non-refiable.

The original error is caused by fact that Object is not compatible with Map<> type and there is no such thing like covariant/contravariant types in java (unlike scala for example).

But there gonna be a problems if dataList contains not maps.

Solution 2

List<Object> and List<Map<String, Object>> are not compatible types, therefore you can't directly cast one to the other. You would have to first cast to to a common supertype and then downcast:

List<Map<String, Object>> rxData = (List<Map<String, Object>>)(List<?>)dataList;

Note that you'll still get a warning at compile time, because it's an unsafe cast.

Share:
11,491

Related videos on Youtube

Andy
Author by

Andy

I use programming as a tool for creating new systems and services. I know most modern programming languages, but I am not programmer; I focus on functionality and the invention of new products. I am expert (PhD) in most aspects of Information Technology, with 20 years experience in Research and Development. Ask me about ad hoc networking, mesh networking and delay tolerant networks, or any networking specific technology, and I will know the answer. I also work with security and encryption. My preferred languages are Java and C as mostly create web services, network protocols and backend services. With these two, or combined, you can create almost anything unless you platform or environment prevents you from it (iOS). I am comfortable with Python, and use it for advanced administrative purposes; starting and stopping services, boot up VMs, databases etc. Python is very powerful, and while it can be used for creating great small programs, I believe it should preferably not be used that way, but rather as a super scripting language. For text parsing, and other parsing tasks, I mainly use Perl, but may sometimes also use Python for this. I have written lots of code in C++, but I really do not like it.

Updated on July 13, 2022

Comments

  • Andy
    Andy almost 2 years

    I should know this, but I can't understand this for some reason.

    Why can I not cast a List of Objects List<Object> to a List of Maps List<Map<String, Object>>? Every object in the list is an object of type Map<String, Object>, so why is the casting not possible?

    What I can do is create a new ArrayList<Map<String, Object>>(); and iterate over the list and add each item with a cast.

    List<Object> dataList;
    ..
    //Why doesn't this work?
    List<Map<String, Object>> rxData = (List<Map<String, Object>>)dataList;
    
    //This works, but is there a better way?
    rxData = new ArrayList<Map<String, Object>>();
    for (Object data : dataList) {
        rxData.add((Map<String, Object>)data);
    }
    
  • k5_
    k5_ almost 8 years
    No need for a raw type here, and the problem is not related to reifiablity.
  • vsminkov
    vsminkov almost 8 years
    @k5_ agree. thanks! but anyway that will be unchecked cast. I'll edit my answer
  • Andy
    Andy almost 8 years
    Thank you! OK, I think I understand, and I need to careful so that the list does not contain anything besides maps.
  • ruakh
    ruakh almost 8 years
    You should remove the first sentence of your answer, or move it later in the answer: it's true, but not very relevant. (Non-reifiability is why a cast from List<?> to List<Map<String, Object>> is unchecked; but it has nothing to do with why you can't cast from List<Object> to List<Map<String, Object>>.)
  • vsminkov
    vsminkov almost 8 years
    @ruakh you are right. thank you! I'll edit my answer
  • Lew Bloch
    Lew Bloch almost 8 years
    Per Joshua Bloch, if you must use @SuppressWarnings("‌​unchecked"), then you must also comment the code there documenting how it is that you are certain that the cast is safe.
  • ruakh
    ruakh almost 8 years
    @LewBloch: I agree; though note that this cast actually isn't safe in the way that Joshua Bloch means, so instead of a comment documenting why it's safe, it should have a comment documenting why it's being used despite not being safe.