title: How to iterate List<Map<String, Object>> and add the key and value dynamically in another hash map in java
body: <p>I'm trying to iterate the <code>List&lt;Map&lt;String, Object&gt;&gt;</code> and want to check if the <strong>code</strong> is "approved" or not - if <strong>code</strong> is having value <strong>"approved"</strong> then I would like to add <strong>"id"</strong> as <strong>Key</strong> and <strong>"date"</strong> as <strong>Value</strong> in another hashMap. </p>
  
  <pre><code>List&lt;Map&lt;String, Object&gt;&gt; prodIds = ((List&lt;Map&lt;String, Object&gt;&gt;) myIds.get("result"));
  </code></pre>
  
  <p><strong>This prodIds returns below set of records:</strong></p>
  
  <pre><code>[{id=[14766724], Date=[1999-01-01]}, {id=[49295837], code=[approved], Date=[2003-04-01]}]
  
  [{id=[58761474621], code=[approved], Date=[2017-09-30]}, {id=[3368781], code=[Cancelled], Date=[2014-01-01]}, {id=[48843224], code=[Cancelled], Date=[2009-01-01]}]
  </code></pre>
  
  <p>I want the output something like this: If code is "approved" - my new hash map should have value like below:</p>
  
  <pre><code>map.put("49295837", "2003-04-01")
  map.put("58761474621", "2017-09-30")
  </code></pre>
  
  <p><strong>Java Code</strong></p>
  
  <pre><code>List&lt;Map&lt;String, Object&gt;&gt; prodIds = ((List&lt;Map&lt;String, Object&gt;&gt;) myIds.get("result"));
  System.out.println("prodIds : " +prodIds ); 
  //  [{id=[14766724], Date=[1999-01-01]}, {id=[49295837], code=[approved], Date=[2003-04-01]}]
  // [{id=[58761474621], code=[approved], Date=[2017-09-30]}, {id=[3368781], code=[Cancelled], Date=[2014-01-01]}, {id=[48843224], code=[Cancelled], Date=[2009-01-01]}]
  
  
  Map&lt;String, String&gt; newMap = new HashMap&lt;&gt;();
      for (Map&lt;String, Object&gt; map : prodIds) { 
          for (Map.Entry&lt;String, Object&gt; entry : map.entrySet()) { 
              String key = entry.getKey();
              System.out.println("Key : " +key);
  
              String value = (String) entry.getValue();
               System.out.println(" Value : " +value);
          }
      }
  </code></pre>
  
  <p>I'm having difficulty how to put the key(id) and value(Date) dynamically if the code value is "approved" into new hash map. It would be really helpful if someone can help me with this.</p>
  
  <p>Appreciated your help in advance!</p>
  
  <p>Thanks</p>