Access HashMap with ArrayList for value

1,060 views
Skip to first unread message

Chris F

unread,
Jan 31, 2016, 12:38:02 AM1/31/16
to mybatis-user
I would like to know how I can access a HashMap where the definition is HashMap<Integer,ArrayList<Integer[]>>.  The ArrayList is a 2-D Integer value {1,1}

<insert id="insertTable" parameterType="hashmap">
    INSERT INTO table (col1, col2, col3, col4)
    VALUES (
    <foreach item="value" collection="map" index="key" open="" separator="),(" close="">
        #{key}, #{Date}, #{value[0], #{value[1]}
    </foreach>
    )
</insert>


Guy Rouillier

unread,
Jan 31, 2016, 1:32:03 AM1/31/16
to mybati...@googlegroups.com
Are you saying the ArrayList will always have a single element, and that single element contains an array of integers of length 2?
 
The way you've defined your foreach below, the "value" Object is an item in the HashMap.  So, you'll need something like value.get(0)[0] and value.get(0)[1].  I don't see where your #1/31/2016 is coming from.
 
--
Guy Rouillier
 
 
 
------ Original Message ------
From: "Chris F" <cdfr...@gmail.com>
To: "mybatis-user" <mybati...@googlegroups.com>
Sent: 1/30/2016 10:06:47 PM
Subject: Access HashMap with ArrayList for value
 
I would like to know how I can access a HashMap where the definition is HashMap<Integer,ArrayList<Integer[]>>.  The ArrayList is a 2-D Integer value {1,1}

<insert id="insertTable" parameterType="hashmap">
    INSERT INTO table (col1, col2, col3, col4)
    VALUES (
    <foreach item="value" collection="map" index="key" open="" separator="),(" close="">
        #{key}, #1/31/2016, #{value[0], #{value[1]}
    </foreach>
    )
</insert>


--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
This email has been sent from a virus-free computer protected by Avast.
www.avast.com

Guy Rouillier

unread,
Jan 31, 2016, 1:34:10 AM1/31/16
to mybati...@googlegroups.com
Hah, emClient replace "#\{Date\}" with today's date.  Trying to escape that.  At any rate, hopefully you get what I'm trying to say.

Chris F

unread,
Feb 1, 2016, 1:59:45 PM2/1/16
to mybatis-user
Thank you for your response.  The HashMap will have 'n' number of values:

HashMap: { {1,{1,2}, { 2,{1,2}, ... }.  I have the HashMap working find in my Java app, but when I define the SQL I am not sure how to define the value containing the multiple aray list.  The goal is to perform batch inserts using the map collection. 

Chris

Chris F

unread,
Feb 17, 2016, 1:14:38 PM2/17/16
to mybatis-user
Any insight how I can access an array "value" in a HashMap within the xml definition?  As mentioned my object will contain a key, value pair with the value being a double array: HashMap: { {1,{1,2}, { 2,{1,2}, ... }.

The #{value[0]} obviously is the wrong syntax.

file.java:
   
void insertTab1(@Param("map") HashMap<Integer, ArrayList<Integer[]>> map, @Param("Date") String Date);


file
.xml:

<insert id="insertTab1" parameterType="hashmap">
    INSERT INTO table1
(id, month, val1, val2)

    VALUES
(
   
<foreach item="value" collection="map" index="key" open="" separator="),(" close="">
       
#{key}, #{Date}, #{value[0]}, #{value[1]}
   
</foreach>
    )
</
insert>

Guy Rouillier

unread,
Feb 18, 2016, 2:47:52 AM2/18/16
to mybati...@googlegroups.com
 
I searched for "mybatis nest foreach".  You didn't provide your table or your Java code, so I used an example I already had handy, but adapted your insert statement.  Here is what my insert statement looks like in the mapper.xml:
 
 <insert id="insertStats" parameterType="map">
  insert into stats
   (
   hub,
   id,
   far_id
   )
  values
   (
   <foreach item="element" collection="map.entrySet()" index="key" open="" separator="),(" close="">
    <foreach item="item" collection="element.value" index="key" open="" separator="),(" close="">
          #{element.key}, #{item[0]}, #{item[1]}
       </foreach>
      </foreach>
   )
 </insert>
 
Here is the data I gave it:
 
         HashMap<String, ArrayList<Integer[]>> map = new HashMap<String, ArrayList<Integer[]>>();
         ArrayList<Integer[]> arrayList = new ArrayList<Integer[]>();
         arrayList.add(new Integer[]{1, 1});
         arrayList.add(new Integer[]{2, 2});
         map.put("phlb", arrayList);
You need the nested foreach because the value in your HashMap is an ArrayList, so you need to iterate over it.
 
--
Guy Rouillier
 
 
 
------ Original Message ------
From: "Chris F" <cdfr...@gmail.com>
To: "mybatis-user" <mybati...@googlegroups.com>
Sent: 2/17/2016 1:14:37 PM
Subject: Re: Access HashMap with ArrayList for value
 
Any insight how I can access an array "value" in a HashMap within the xml definition?  As mentioned my object will contain a key, value pair with the value being a double array: HashMap: { {1,{1,2}, { 2,{1,2}, ... }.


The #{value[0]} obviously is the wrong syntax.

file.java:
   
void insertTab1(@Param("map") HashMap<Integer, ArrayList<Integer[]>> map, @Param("Date") String Date);


file
.xml:

<insert id="insertTab1" parameterType="hashmap">
    INSERT INTO table1
(id, month, val1, val2)
    VALUES
(
   
<foreach item="value" collection="map" index="key" open="" separator="),(" close="">

       
#{key}, #2/18/2016, #{value[0]}, #{value[1]}
   
</foreach>
    )
</
insert>



On Monday, February 1, 2016 at 11:59:45 AM UTC-7, Chris F wrote:
Thank you for your response.  The HashMap will have 'n' number of values:

HashMap: { {1,{1,2}, { 2,{1,2}, ... }.  I have the HashMap working find in my Java app, but when I define the SQL I am not sure how to define the value containing the multiple aray list.  The goal is to perform batch inserts using the map collection. 

Chris

On Saturday, January 30, 2016 at 10:38:02 PM UTC-7, Chris F wrote:
I would like to know how I can access a HashMap where the definition is HashMap<Integer,ArrayList<Integer[]>>.  The ArrayList is a 2-D Integer value {1,1}

<insert id="insertTable" parameterType="hashmap">
    INSERT INTO table (col1, col2, col3, col4)
    VALUES (
    <foreach item="value" collection="map" index="key" open="" separator="),(" close="">
        #{key}, #2/18/2016, #{value[0], #{value[1]}
    </foreach>
    )
</insert>


--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mybatis-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris F

unread,
Mar 20, 2016, 4:47:08 PM3/20/16
to mybatis-user, guy.ro...@gmail.com
Guy,

  Thanks for the reply.  The problem with the example you provided including the one in the link is the element.value on the second foreach is throwing out an error saying it does not recognize it.  I can do something like map.values() with no error, but ultimately it is returning back the wrong information and the size of my data causes an Out of Memory error. 

  In your example below, the Batis framework doesn't know how to deal with the element.value.  On the second foreach does it somehow need to switch over to searching a ArrayList (list)?  I would like to get this working, but in the meantime I have worked around the issue with two separate insert/update queries (not efficient in my opinion).


 <insert id="insertStats" parameterType="map">
  insert into stats
   (
   hub,
   id,
   far_id
   )
  values
   (
   
<foreach item="element" collection="map.entrySet()" index="key" open="" separator="),(" close="">
   
<foreach item="item" collection="element.value" index="key" open="" separator="),(" close="">
          #{element.key}, #{item[0]}, #{item[1]}
       
</foreach>
     
</foreach>
   )
 
</insert>



Guy Rouillier

unread,
Mar 21, 2016, 3:25:32 AM3/21/16
to mybatis-user
Chris, I've run this code and it definitely works.  Notice in the outer foreach, that the collection is created with map.entrySet().  entrySet() is defined as Set<Map.Entry<K,V>>.  "element" in the outer foreach is an object from this set, i.e., Map.Entry<K,V>.  If you look in the Java API at the definition of interface Map.Entry<K,V>, you'll see functions for getKey() and getValue().  So, in the inner foreach, element.value most certainly exists.  As shown in the Java code, it's value will be ArrayList<Integer[]>.  So, finally then, and item from this second collection will be simply Integer[].
 
Perhaps you overlooked the invocation of map.entrySet() in the definition of the collection in the outer foreach?  Here is a table definition for PostgreSQL if you want to run the provided sample.
 
CREATE TABLE stats
(
  hub character varying(20),
  id bigint,
  far_id bigint
);
0 viruses found. www.avast.com
Reply all
Reply to author
Forward
0 new messages