java中有两个list列表,尽量少的去循环

news/2024/10/2 7:39:48 标签: java, list, 开发语言

java中有两个list列表,一个list列表是paymentRecord,另外一个listlistApplyBase,paymentRecord中的lendCode字段值跟listApplyBase中的repaymentCode字段值是对应的,用stream流去循环paymentRecord列表,然后判断当paymentRecord中的lendCode字段值跟listApplyBase中的repaymentCode字段值相等时打印listApplyBase中的信息,尽量少的去循环。

为了实现你的需求,我们可以使用Java Stream API来高效地处理两个列表的匹配问题。具体步骤如下:

listApplyBase 转换为 Map:将 listApplyBase 转换为一个 Map,其中键是 repaymentCode,值是 repaymentApply 对象。这样可以在后续的匹配过程中快速查找。
使用 Stream API 遍历 paymentRecord 列表:在遍历 paymentRecord 列表时,使用 Map 来快速查找匹配的 repaymentApply 对象。
示例代码
假设你有两个类 paymentRecord 和 repaymentApply,它们的定义如下:

Java
深色版本
class paymentRecord {
    private String lendCode;
    private String otherField1;
    private double amount;

    // 构造函数
    public paymentRecord(String lendCode, String otherField1, double amount) {
        this.lendCode = lendCode;
        this.otherField1 = otherField1;
        this.amount = amount;
    }

    // Getter 和 Setter 方法
    public String getlendCode() {
        return lendCode;
    }

    public void setlendCode(String lendCode) {
        this.lendCode = lendCode;
    }

    public String getOtherField1() {
        return otherField1;
    }

    public void setOtherField1(String otherField1) {
        this.otherField1 = otherField1;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    @Override
    public String toString() {
        return "paymentRecord{" +
                "lendCode='" + lendCode + '\'' +
                ", otherField1='" + otherField1 + '\'' +
                ", amount=" + amount +
                '}';
    }
}

class repaymentApply {
    private String repaymentCode;
    private String otherField1;
    private int otherField2;

    // 构造函数
    public repaymentApply(String repaymentCode, String otherField1, int otherField2) {
        this.repaymentCode = repaymentCode;
        this.otherField1 = otherField1;
        this.otherField2 = otherField2;
    }

    // Getter 和 Setter 方法
    public String getrepaymentCode() {
        return repaymentCode;
    }

    public void setrepaymentCode(String repaymentCode) {
        this.repaymentCode = repaymentCode;
    }

    public String getOtherField1() {
        return otherField1;
    }

    public void setOtherField1(String otherField1) {
        this.otherField1 = otherField1;
    }

    public int getOtherField2() {
        return otherField2;
    }

    public void setOtherField2(int otherField2) {
        this.otherField2 = otherField2;
    }

    @Override
    public String toString() {
        return "repaymentApply{" +
                "repaymentCode='" + repaymentCode + '\'' +
                ", otherField1='" + otherField1 + '\'' +
                ", otherField2=" + otherField2 +
                '}';
    }
}
主要逻辑
Java
深色版本
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Main {
    public static void main(String[] args) {
        // 假设你有两个服务类,可以从数据库中获取数据
        List<paymentRecord> paymentRecord = opPaymentService.list();
        List<repaymentApply> listApplyBase = opRepaymentService.list();

        // 将listApplyBase转换为Map
        Map<String, repaymentApply> applyBaseMap = listApplyBase.stream()
                .collect(Collectors.toMap(
                        repaymentApply::getrepaymentCode, // 生成Map的键
                        user -> user                              // 生成Map的值
                ));

        // 使用Stream API遍历paymentRecord列表
        paymentRecord.stream()
                .filter(record -> applyBaseMap.containsKey(record.getlendCode()))
                .forEach(record -> {
                    repaymentApply applyBase = applyBaseMap.get(record.getlendCode());
                    log.info("Matching repaymentApply: {}", applyBase);
                });
    }
}
详细解释
获取数据:
paymentRecord 和 listApplyBase 分别从 opPaymentService 和 opRepaymentService 获取数据。
listApplyBase 转换为 Map:
使用 stream() 方法创建一个流。
使用 collect 方法将流中的元素收集到一个 Map 中。
Collectors.toMap 方法接受两个参数:
第一个参数是一个函数,用于生成 Map 的键。在这个例子中,我们使用 repaymentApply::getrepaymentCode 方法引用,它返回 repaymentApply 对象的 repaymentCode 字段。
第二个参数是一个函数,用于生成 Map 的值。在这个例子中,我们使用 user -> user,即将 repaymentApply 对象本身作为值。
使用 Stream API 遍历 paymentRecord 列表:
使用 stream() 方法创建一个流。
使用 filter 方法过滤出 lendCode 在 applyBaseMap 中存在的 paymentRecord 对象。
使用 forEach 方法遍历过滤后的 paymentRecord 对象,并打印匹配的 repaymentApply 对象的信息。
处理键冲突
如果 listApplyBase 中有多个 repaymentApply 对象具有相同的 repaymentCode,Collectors.toMap 方法会抛出 IllegalStateException。为了避免这种情况,可以使用 Collectors.toMap 的另一个重载方法,指定如何处理键冲突:

Java
深色版本
Map<String, repaymentApply> applyBaseMap = listApplyBase.stream()
        .collect(Collectors.toMap(
                repaymentApply::getrepaymentCode, // 生成Map的键
                user -> user,                             // 生成Map的值
                (existingValue, newValue) -> existingValue // 如果有冲突,保留第一个值
        ));
在这个例子中,如果 repaymentCode 有冲突,Map 将保留第一个遇到的 repaymentApply 对象。

通过这种方式,你可以高效地处理两个列表的匹配问题,减少不必要的循环次数。希望这些示例能帮助你解决问题。


http://www.niftyadmin.cn/n/5688163.html

相关文章

什么是信息增益比

信息增益比&#xff08;Information Gain Ratio&#xff09; 是对 信息增益&#xff08;Information Gain, IG&#xff09; 的改进&#xff0c;它考虑了特征的不同取值数量对信息增益的影响&#xff0c;避免了信息增益偏向于取值较多特征的倾向。信息增益比常用于构建决策树&am…

如果您忘记了 Apple ID 和密码,按照指南可重新进入您的设备

即使您的 iPhone 或 iPad 由于各种原因被锁定或禁用&#xff0c;也可以使用 iTunes、“查找我的”、Apple 支持和 iCloud 解锁您的设备。但是&#xff0c;此过程需要您的 Apple ID 和密码来验证所有权并移除激活锁。如果您忘记了 Apple ID 和密码&#xff0c;请按照我们的指南重…

蓝桥杯—STM32G431RBT6(IIC通信--EEPROM(AT24C02)存储器进行通信)

一、什么是IIC&#xff1f;24C02存储器有什么用&#xff1f; IIC &#xff08;IIC 是半双工通信总线。半双工意味着数据在某一时刻只能沿一个方向传输&#xff0c;即发送数据的时候不能接收数据&#xff0c;接收数据的时候不能发送数据&#xff09;即集成电路总线&#xff08;…

根据视频id查询播放量

声明&#xff1a;文章仅用于学习交流,如有侵权请联系删除 如何根据视频ID查询视频的播放数量 在数字化时代&#xff0c;视频内容的消费已成为人们日常生活的重要组成部分。无论是社交媒体平台上的短视频&#xff0c;还是视频分享网站上的长视频&#xff0c;了解视频的播放数量…

SpringGateway(网关)微服务

一.启动nacos 1.查看linux的nacos是否启动 docker ps2.查看是否安装了nacos 前面是你的版本&#xff0c;后面的names是你自己的&#xff0c;我们下面要启动的就是这里的名字。 docker ps -a3.启动nacos并查看是否启动成功 二.创建网关项目 1.创建idea的maven项目 2.向pom.x…

如何避免回溯算法中的回溯陷阱?

如何避免回溯算法中的回溯陷阱&#xff1f; 回溯算法是一种强大的问题解决方法&#xff0c;但在使用过程中也容易陷入一些陷阱。这些陷阱可能导致算法效率低下、陷入无限循环或者无法找到正确的解决方案。在本文中&#xff0c;我们将探讨如何避免回溯算法中的回溯陷阱&#xf…

CSS全解析

文章目录 CSS全解析一、CSS是什么二、基本语法规范三、引入方式&#xff08;一&#xff09;内部样式表&#xff08;二&#xff09;行内样式表&#xff08;三&#xff09;外部样式 四、代码风格&#xff08;一&#xff09;样式格式&#xff08;二&#xff09;样式大小写&#xf…

Python或R时偏移算法实现

&#x1f3af;要点 计算单变量或多变量时序距离&#xff0c;使用欧几里得、曼哈顿等函数量化不同时序差异。量化生成时序之间接近度相似性矩阵。使用高尔距离和堪培拉距离等相似度测量。实现最小方差匹配算法&#xff0c;绘制步进模式的图形表示。其他语言包算法实现。 &…