1

I really need assistance in solving the below

I have two List<Map<String, Object>> map1 and List<Map<String, Object>> map2, I need to combine two Map to create the List<Map<String, Object>> map3. I am really struggling to make it working. Any real quick help ?

Data to work with ? 1st Map

[{ACC_NUM=4973, PARENT_ACC_ID=7896, CRTE_DT=2017-06-22 , PURCH_DT=null, CRTE_USER_ID=ZZZ, IN_CHANGED=NO}, 
{ACC_NUM=5464, PARENT_ACC_ID=5464, CRTE_DT=2017-06-22 , PURCH_DT=null, CRTE_USER_ID=ZZZ, IN_CHANGED=NO}, 
{ACC_NUM=8313, PARENT_ACC_ID=5902, CRTE_DT=2017-06-22 , PURCH_DT=null, CRTE_USER_ID=ZZZ, IN_CHANGED=NO}, 
{ACC_NUM=8693, PARENT_ACC_ID=1268, CRTE_DT=2017-06-22 , PURCH_DT=null, CRTE_USER_ID=ZZZ, IN_CHANGED=NO}, 
{ACC_NUM=12175, PARENT_ACC_ID=12175, CRTE_DT=2017-06-22 , PURCH_DT=null, CRTE_USER_ID=ZZZ, IN_CHANGED=NO}]

2nd Map

[{COUNT=2, PRNT_ACC_NUM=5464}, {COUNT=2, PRNT_ACC_NUM=1268}, {COUNT=5, PRNT_ACC_NUM=5902}, {COUNT=1, PRNT_ACC_NUM=7896}]

create 3rd Map

[{ACC_NUM=4973, PARENT_ACC_ID=7896, CRTE_DT=2017-06-22 , PURCH_DT=null, CRTE_USER_ID=ZZZ, IN_CHANGED=NO, **COUNT=1**}, 
{ACC_NUM=5464, PARENT_ACC_ID=5464, CRTE_DT=2017-06-22 , PURCH_DT=null, CRTE_USER_ID=ZZZ, IN_CHANGED=NO, **COUNT=2**}, 
{ACC_NUM=8313, PARENT_ACC_ID=5902, CRTE_DT=2017-06-22 , PURCH_DT=null, CRTE_USER_ID=ZZZ, IN_CHANGED=NO, **COUNT=5**}, 
{ACC_NUM=8693, PARENT_ACC_ID=1268, CRTE_DT=2017-06-22 , PURCH_DT=null, CRTE_USER_ID=ZZZ, IN_CHANGED=NO, **COUNT=2**}, 
{ACC_NUM=12175, PARENT_ACC_ID=12175, CRTE_DT=2017-06-22 , PURCH_DT=null, CRTE_USER_ID=ZZZ, IN_CHANGED=NO}]

Code

Map<String, Object> map1 = new HashMap<>();
    map1.put("ACC_NUM", 4973);
    map1.put("PARENT_ACC_ID", 7896);
    map1.put("CRTE_DT", 2017-06-22);
    map1.put("PURCH_DT", null);
    map1.put("CRTE_USER_ID", "ZZZ");
    map1.put("IN_CHANGED", "NO");

    Map<String, Object> map12 = new HashMap<>();
    map12.put("ACC_NUM", 5464);
    map12.put("PARENT_ACC_ID", 5464);
    map12.put("CRTE_DT", 2017-06-22);
    map12.put("PURCH_DT", null);
    map12.put("CRTE_USER_ID", "ZZZ");
    map12.put("IN_CHANGED", "NO");

    List<Map<String, Object>> l1 = new ArrayList<>();
    l1.add(map1);
    l1.add(map12);

    Map<String, Object> map21 = new HashMap<>();
    map21.put("COUNT", 2);
    map21.put("PRNT_ACC_NUM", 7896);

    Map<String, Object> map22 = new HashMap<>();
    map22.put("COUNT", 2);
    map22.put("PRNT_ACC_NUM", 7896);

    List<Map<String, Object>> l2 = new ArrayList<>();
    l2.add(map21);
    l2.add(map22);
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
Jeff Cook
  • 7,956
  • 36
  • 115
  • 186
  • What specifically are you having trouble with? What's going wrong with what you've tried so far? – mypetlion Jul 26 '18 at 20:21
  • Have you tried using [`Map.putAll(Map)`](https://docs.oracle.com/javase/10/docs/api/java/util/Map.html#putAll(java.util.Map)) or [`Map.merge(Object,Object,BiFunction)`](https://docs.oracle.com/javase/10/docs/api/java/util/Map.html#merge(K,V,java.util.function.BiFunction))? – Slaw Jul 26 '18 at 20:31
  • @shmosel - Its not duplicate and that answer doesnot works – Jeff Cook Jul 26 '18 at 20:59
  • Why isn't it a duplicate? – shmosel Jul 26 '18 at 21:00
  • Did you try to run code ? Did it work for you? – Jeff Cook Jul 26 '18 at 21:01
  • @Prateek try this guy --> `List> map3 = new ArrayList<>(); l1.forEach(map -> { Map temp = new HashMap<>(map); temp.put("COUNT", l2.stream() .filter(m -> m.containsValue(map.get("PARENT_ACC_ID"))) .findFirst().map(c -> c.get("COUNT")).orElse("SomeDefaultValue......")); map3.add(temp); });` – Ousmane D. Jul 26 '18 at 21:37

0 Answers0