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?

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Kshitiz Sharma
  • 17,947
  • 26
  • 98
  • 169
  • http://stackoverflow.com/questions/10925648/how-to-convert-arraylist-containing-complex-type-to-an-array – jmj Jun 07 '12 at 04:51
  • @JigarJoshi That is my own question that was closed before I could get an answer. – Kshitiz Sharma Jun 07 '12 at 04:51
  • There are correct answer in the comment in your previous question: No array with parameterized type – Adrian Shum Jun 07 '12 at 04:55
  • @AdrianShum Oh really??? I wonder what Spring developers were thinking.. – Kshitiz Sharma Jun 07 '12 at 04:56
  • There is nothing to do with Spring I believe?! – Adrian Shum Jun 07 '12 at 04:58
  • @AdrianShum What it has to with Spring is that Spring developers used array with parametrized type, that you say is not possible. But my bad, maybe they have Chuck Norris working for them. – Kshitiz Sharma Jun 07 '12 at 05:00
  • possible duplicate of [Problem with map array with generics](http://stackoverflow.com/questions/7343286/problem-with-map-array-with-generics) – KV Prajapati Jun 07 '12 at 05:04
  • I think I am too brief on the reply coz I thought the comment in previous question is already quite clear. There are quite some situations that array and generics doesn't live well together. This case is one of them. – Adrian Shum Jun 07 '12 at 05:23

6 Answers6

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.

Gene
  • 46,253
  • 4
  • 58
  • 96
2

Try this,

 Map<String,Object>[] ar=list.toArray(new HashMap[list.size()]);
KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
  • 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. – Alex Calugarescu Jun 07 '12 at 05:16
1

You can try like this:

Map<String, Object> [] mp = list.toArray(new HashMap[list.size()]);
UVM
  • 9,776
  • 6
  • 41
  • 66
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}
    }
}
Lai Xin Chu
  • 2,462
  • 15
  • 29
0

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

liuzhijun
  • 4,329
  • 3
  • 23
  • 27
-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.

MTilsted
  • 5,425
  • 9
  • 44
  • 76
  • What about it? Do you have an example where Spring does this? – MTilsted Jun 07 '12 at 05:16
  • int[] org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.batchUpdate(String sql, Map[] batchValues) – Kshitiz Sharma Jun 07 '12 at 05:19
  • 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. – MTilsted Jun 07 '12 at 08:20