4

I have this ArrayList in Java -

List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();

To convert it to an array I invoke list.toArray() method, but it returns Object[]. This is not what I want. I want Map<String, Object>[].

I know about List.toArray(T[] a); It doesn't work with parameterized types.

The method signature of batchUpdate method in Spring framework is this -

int[] org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.batchUpdate(String sql, Map<String, ?>[] batchValues)

If it is not possible to create array of Map objects why is Spring using it? And how are we supposed to use this method then?

8
  • stackoverflow.com/questions/10925648/… Commented Jun 7, 2012 at 4:51
  • @JigarJoshi That is my own question that was closed before I could get an answer. Commented Jun 7, 2012 at 4:51
  • There are correct answer in the comment in your previous question: No array with parameterized type Commented Jun 7, 2012 at 4:55
  • @AdrianShum Oh really??? I wonder what Spring developers were thinking.. Commented Jun 7, 2012 at 4:56
  • There is nothing to do with Spring I believe?! Commented Jun 7, 2012 at 4:58

6 Answers 6

3

In a nutshell, you can't make arrays of concrete parameterized types. This is a pretty good explanation of what's going on. The Spring type is essentially the same as saying Map batchValues. The parameter types are for documentation only. This gaping hole in the Java type system is a tradeoff for performance.

Sign up to request clarification or add additional context in comments.

Comments

2

Try this,

 Map<String,Object>[] ar=list.toArray(new HashMap[list.size()]);

1 Comment

This is the way to go... But you still lose type-safety by doing this... I think it would be more appropriate to use new Map[list.size()], this way any Map added to the list will go just fine.
1

Do it this way:

Map<String, Object> [] mp = new HashMap[list.size()]; 
list.toArray(mp); 

This answer works. I tested it.

My full test code is as follows:

import java.util.*;
public class Test {

    public static void main (String [] args) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("Hello", "World");
        ArrayList<Map<String, Object>> list = new ArrayList<Map <String, Object>>();
        list.add(map);

        Map<String, Object> [] mp = new HashMap[list.size()]; 
        list.toArray(mp);   
        System.out.println(mp[0]);    // prints out {Hello=World}
    }
}

Comments

1

You can try like this:

Map<String, Object> [] mp = list.toArray(new HashMap[list.size()]);

Comments

0

HashMap[] map = list.toArray(new HashMap[0]);

Comments

-2

You can't do it.

List.toArray(T[] a) is your only option, but it does not work with parameterized types because they are not preserved at runtime.

3 Comments

What about it? Do you have an example where Spring does this?
int[] org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.batchUpdate(String sql, Map<String, ?>[] batchValues)
Hu? That spring function does something completely different. The problem which prevent the original author from doing what he want, is is that the generic part of the type don't exists at runtime. So you can't create a new object of the same type.

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.