Results 1 to 5 of 5
  1. #1
    lalossa is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default how to create an array of maps

    Hi,
    I would like to use simpleJdbcInsert class and executeBatch method

    public int[] executeBatch(Map<String,Object>[] batch)

    So I need to pass an array of Map<String,Object> as paramter. How to create such an array?
    What I tried is
    Map<String, Object>[] myArray = new HashMap<String, Object>[10]

    It is error: Cannot create generic array of Map<String, Object>

    So how to create an array of Map<String, Object> ?
    Thanks

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default Re: how to create an array of maps

    Yep! You cannot create an array of a generic type. It will not be typesafe! (Effective Java 2nd edition, Joshua Bloch, Item 25). However, you can create a list of a generic type. Lists are more flexible than arrays anyway. So try the following:

    Java Code:
    List<HashMap<String,Object>> myList = new ArrayList<HashMap<String, Object>>();
    If you don't need any methods other than those provided by the interface, then use the interface as the type. Otherwise, use the class. Compare to the following:

    Java Code:
    ArrayList<HashMap<String, Object>> myList = new ArrayList<HashMap<String, Object>>();
    Since you didn't include the original classes and I am not familiar with the class and method you describe, I do not know if this is home grown or a well defined class. If the class requires an array Map then perhaps you can just declare it without the type parameters and hope the class is well-behaved and also be careful to use the appropriate key/value pairs that the foreign method expects.

    Regards,
    Jim
    Last edited by jim829; 02-16-2013 at 11:07 PM. Reason: Added Edition Number on cited reference

  3. #3
    lalossa is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default Re: how to create an array of maps

    Hi Jim,

    thanks for the answer. Yes, a List would be easier, buti need to pass a map<String, Object>[] as parameter. (SimpleJdbcInsert (Spring Framework API 2.5))
    Any ideas how can I do it?


    Quote Originally Posted by jim829 View Post
    Yep! You cannot create an array of a generic type. It will not be typesafe! (Effective Java 2nd edition, Joshua Bloch, Item 25). However, you can create a list of a generic type. Lists are more flexible than arrays anyway. So try the following:

    Java Code:
    List<HashMap<String,Object>> myList = new ArrayList<HashMap<String, Object>>();
    If you don't need any methods other than those provided by the interface, then use the interface as the type. Otherwise, use the class. Compare to the following:

    Java Code:
    ArrayList<HashMap<String, Object>> myList = new ArrayList<HashMap<String, Object>>();
    Since you didn't include the original classes and I am not familiar with the class and method you describe, I do not know if this is home grown or a well defined class. If the class requires an array Map then perhaps you can just declare it without the type parameters and hope the class is well-behaved and also be careful to use the appropriate key/value pairs that the foreign method expects.

    Regards,
    Jim

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,341
    Rep Power
    5

    Default Re: how to create an array of maps

    Well, this will work. I did not get any unchecked exception warnings that I had to suppress.

    Java Code:
      public static void main(String[] args) {
          HashMap<?,?>[] foo = new HashMap[2];
          HashMap<String,Object> map1 = new HashMap<String, Object>();
          map1.put("a", "myObjecta");
          HashMap<String,Object> map2 = new HashMap<String, Object>();
          map2.put("a",  "myObjectb");
          foo[0] = map1;
          foo[1] = map2;
          
          for (HashMap<?,?> h : foo) {
             System.out.println(h.get("a"));
          }
       }
    Regards,
    Jim

  5. #5
    lalossa is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default Re: how to create an array of maps

    Hi,

    thanks, that almost works. I cannot pass foo to a method that excpets Map<String,Object>[] as parameter. Casting is needed like (Map<String, Object>[]) foo and then it works ( @SuppressWarnings("unchecked") is also needed)

    Thanks for the help!

    Quote Originally Posted by jim829 View Post
    Well, this will work. I did not get any unchecked exception warnings that I had to suppress.

    Java Code:
      public static void main(String[] args) {
          HashMap<?,?>[] foo = new HashMap[2];
          HashMap<String,Object> map1 = new HashMap<String, Object>();
          map1.put("a", "myObjecta");
          HashMap<String,Object> map2 = new HashMap<String, Object>();
          map2.put("a",  "myObjectb");
          foo[0] = map1;
          foo[1] = map2;
          
          for (HashMap<?,?> h : foo) {
             System.out.println(h.get("a"));
          }
       }
    Regards,
    Jim

Similar Threads

  1. Create an array of polygons
    By jtech55 in forum Java 2D
    Replies: 0
    Last Post: 03-18-2011, 07:49 PM
  2. Create a Get/set for an array
    By mechdesignron in forum New To Java
    Replies: 4
    Last Post: 05-04-2010, 09:39 PM
  3. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 09:01 PM
  4. how create array graphically
    By sunolinu in forum AWT / Swing
    Replies: 3
    Last Post: 02-12-2010, 07:34 AM
  5. Create an array of Vectors
    By nwboy74 in forum Advanced Java
    Replies: 1
    Last Post: 11-12-2008, 08:00 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •