【问题标题】:ArrayList put a field in each map of the list according to the value of another arrayListArrayList根据另一个arrayList的值在list的每个map中放一个字段
【发布时间】:2019-09-16 10:43:13
【问题描述】:

我有一个数组列表:

{
    "response": [
        {
            "number": "229425941           ",
            "CURRENCY": "EUR",
            "account": "0026.5501.90.0490520505",
            "branch": "0154",
            "product": "geia",
            "service": "0026.0002.62.0300162968",
            "amount": 20000,
            "expDate": "2019-09-20",
            "bank": "0026",
            "name": "A name        "
        },
        {
            "number": "229425941           ",
            "CURRENCY": "EUR",
            "account": "0026.5501.90.0490520505",
            "branch": "0154",
            "product": "geia",
            "service": "0026.0002.62.0300162968",
            "amount": 20000,
            "expDate": "2019-09-20",
            "bank": "0027",
            "name": "A name        "
        },
    ]
}

我有第二个数组列表,其中包含每家银行的详细信息。名单是这样的:

"details": [
        {
            "bankCode": "010",
            "bankDescription": "BANK",
        },
        {
            "bankCode": "011",
            "bankDescription": "NATIONAL",
        },
        {
            "bankCode": "012",
            "bankDescription": "ALPHA",
        }
    ]
}

现在我想遍历第一个列表,并根据第二个列表的bankCodebankDescriptionbankDescription 字段放入列表的每个对象中。我为第一个 arrayList 制作了一个流:

firtsList.stream().forEach(a -> {
            a.put();
        });

我的猜测是以某种方式遍历第二个列表,但我不知道有效的方法。我的输出必须是这样的:

{
    "response": [
        {
            "number": "229425941           ",
            "CURRENCY": "EUR",
            "account": "0026.5501.90.0490520505",
            "branch": "0154",
            "product": "geia",
            "service": "0026.0002.62.0300162968",
            "amount": 20000,
            "expDate": "2019-09-20",
            "bank": "010",
            "bankDesc: "BANK"
            "name": "A name        "
        },
        {
            "number": "229425941           ",
            "CURRENCY": "EUR",
            "account": "0026.5501.90.0490520505",
            "branch": "0154",
            "product": "geia",
            "service": "0026.0002.62.0300162968",
            "amount": 20000,
            "expDate": "2019-09-20",
            "bank": "011",
            "bankDesc: "NATIONAL"
            "name": "A name        "
        },
    ]
}

secondList.stream().filter(bic -> firstList.stream()
                .anyMatch(c -> StringUtils.substring((String) c.get("bankDesc"), 1, 3)
                .equals(bic.get("bankCode"))));

【问题讨论】:

  • 首先,您需要向我们展示您使用 Java 中的哪些数据结构来表示上述 JSON。或者您是否尝试以这种方式编辑 JSON 文档?请在您的问题中包含适用的代码。一般来说,我会将银行信息设为Map 并使用它,但目前您的问题太抽象了。
  • 我使用这些列表: Map p = message.getPayload(); ArrayList> bankDetails = ( ArrayList>) p.get("details"); ArrayList> chequesInCollat​​eralList = (ArrayList>) chequesInCollat​​eralMap.get("response");
  • 他们告诉我对流()使用过滤器
  • 请按edit 链接将信息添加到问题中。不要将 cmets 用于代码,它的格式不正确。请记住通过选择它并按{} 按钮在问题中对其进行格式化。在任何情况下,尽量避免映射到Object 并改用真正的类。而且使用filter 效率会很低。

标签: java loops arraylist java-stream


【解决方案1】:

有一个模型/dto 来简化这个过程。可以使用 objectMapper 将 Json 直接映射到 POJO 中。

假设您无视所有建议并继续使用 Json/Map,您可以按照以下方式继续:

//Build a dictionary from the data
Map<String, String> bankCodeToDescriptionMap = secondList.stream().collect(Collectors.toMap(v -> v.get("bankCode"), v -> v.get("bankDescription")));

firstList.stream().forEach(a -> {
            a.put("bankDescription", bankCodeToDescriptionMap.get(v.get("bankCode")));
        });

不要为每个值遍历列表。太慢了

【讨论】:

  • 我可以对 stream() 使用过滤器来达到预期的效果吗?
  • 还在 v.get("bankDescription") 中显示语法错误
  • 如果使用过滤器,每次都会遍历列表,增加复杂度。添加括号以解决语法错误
猜你喜欢
  • 2022-06-12
  • 2013-12-30
  • 2021-03-04
  • 2021-01-20
  • 2012-04-13
  • 1970-01-01
  • 2010-10-06
  • 2019-04-25
  • 1970-01-01
相关资源
最近更新 更多