如何在java中结合两个不同的Map? [英] How to combine two different Map in java?

查看:143
本文介绍了如何在java中结合两个不同的Map?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我关注了许多此类链接合并两个地图,但尚无法解决问题。

I followed many such links Merging two Maps, but unable to solve the issue yet.

我有两个地图列表<地图<字符串,对象>> - 此地图包含 acc_num,parent_acc_num,crte_dt,rnwl_dt,userid 等,其他地图包含 acc_num isActive (这是第一张地图的更多细化)。

I have two Maps List<Map<String, Object>> - This map contains acc_num, parent_acc_num, crte_dt, rnwl_dt, userid etc and other map contains acc_num and isActive (This is more refinement from 1st map).

我想创建/合并这两张地图并创建最终地图,其中包含 acc_num,parent_acc_num,crte_dt,rnwl_dt,userid + isActive 详情。

I wanted to create/combine these two maps and create final map which conatins acc_num, parent_acc_num, crte_dt, rnwl_dt, userid + isActive details.

我该怎么做?

以下是样本数据。唯一的问题是地图列表,如何迭代一个在另一个?

Below is the sample Data. The only issue is both are List of Map, how to iterate one over another ?

{CRTE_DT=2017-06-22, USER_ID=ABC, ACC_NUM=9449, RNWL_DT=2017-06-22, PARENT_ACC_NO=9449}

{ACC_NUM=9449, IS_ACTIVE=false}

另一个查询:
如果以下工作,则可能不需要进行计算

Another Query: If below works then may not need to do calculations

我有下面的Oracle查询使用12c,我需要检查查询本身的方式

I have the below Oracle query using 12c, I need the way to check in query itself if


  1. 如果CreatedDate + 30天> Sysdate然后显示列有true --->在Trail中

  2. 如果CreatedDate + 30天< Sysdate然后显示列有假--->超出期限




select cust.acc_num,来自CUST_ACCT cust的third.parent_acc_id,cust.crte_dt,cust.updated_dt,cust.crte_user_id,ThirdParty_third第三个
+,其中cust.updated_dt为null,cust.CRTE_DT< sysdate
+和cust .acc_num = third.third_code和is_actv =?union
+选择cust.acc_num,third.parent_acc_id,cust.crte_dt,cust.updated_dt,cust.crte_user_id来自CUST_ACCT cust,ThirdParty_third third
+ where(cust.updated_dt不为null且cust.updated_dt< sysdate)
+和cust.acc_num = third.third_code和is_actv =?;

"select cust.acc_num, third.parent_acc_id, cust.crte_dt,cust.updated_dt ,cust.crte_user_id from CUST_ACCT cust, ThirdParty_third third " + "where cust.updated_dt is null and cust.CRTE_DT < sysdate " + "and cust.acc_num = third.third_code and is_actv = ? union " + "select cust.acc_num, third.parent_acc_id, cust.crte_dt,cust.updated_dt, cust.crte_user_id from CUST_ACCT cust, ThirdParty_third third " + "where ( cust.updated_dt is not null and cust.updated_dt < sysdate ) " + "and cust.acc_num = third.third_code and is_actv = ? ";


推荐答案

假设您的列表只包含两个地图,并且它们都是可变的。当且仅当第二个映射的映射 ACC_NUM 等于第一个映射时,你可以做的是将第二个映射的所有映射添加到第一个映射。

Assuming your list contains only two maps and they are both mutable. What you can do is add all mappings of the second map to the first one if and only if the second map has the mapping ACC_NUM equal to the first one.

final List<Map<String, Object>> maps = ...;

if (Optional.ofNullable(map.get(1).get("ACC_NUM")).filter(acc_num -> acc_num.equals(map.get(0).get("ACC_NUM"))).isPresent())
{
    map.get(0).putAll(map.get(1));
}

编辑

final List<Map<String, Object>> l1 = ...;
final List<Map<String, Object>> l2 = ...;

final List<Map<String, Object>> result = l1.stream()
    .map(map ->
    {
      final Map<String, Object> r = new HashMap<>(map);
      l2.stream()
          .filter(map2 -> Objects.equals(map.get("ACC_NUM"), map2.get("ACC_NUM")))
          .findFirst()
          .map(map2 -> map2.get("IS_ACTIVE"))
          .ifPresent(is_active -> map.put("IS_ACTIVE", is_active));
      return r;
    })
    .collect(Collectors.toList());

这篇关于如何在java中结合两个不同的Map?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆