gpt4 book ai didi

java - 比较 Java 中列表映射中的每个对象

转载 作者:行者123 更新时间:2023-11-30 01:55:13 25 4
gpt4 key购买 nike

我有一个 Java 程序,其 Map 类型为 <String, Project>其中包含所有 Project 对象,并且 Project 的每个对象都包含相关 Process 对象的列表,即 List<Process> .

public class Project {
private String name;
private Path path;
private List<Process> processes;
\\ getters and setters follow

public class Process {
private String name;
private String path;
private String flow;
\\getters and setters follow

我正在编写一个函数来查找 JaroWinklerDistance 之间的字符串距离(使用 flow )每个Process (跨所有项目)。我想出了以下内容:

    public void compareAllProcesses(Map<String, Project> projects) {
Iterator<Project> projIter1 = projects.values().iterator();
while(projIter1.hasNext()) {
Project proj1 = projIter1.next();
Iterator<Process> procIter1 = proj1.getProcesses().iterator();
while(procIter1.hasNext()) {
Process proc1 = procIter1.next();
Iterator<Project> projIter2 = projects.values().iterator();
while(projIter2.hasNext()) {
Project proj2 = projIter2.next();
Iterator<Process> procIter2 = proj2.getProcesses().iterator();
while(procIter2.hasNext()) {
Process proc2 = procIter2.next();
//use JaroWinklerDistance to compare proc1.getFlow() with proc2.getFLow()
}
}
}
}
}

是否有更高效、更优雅的方式来实现这一目标?

最佳答案

也许与Streams:

projects.values()
.stream()
.flatMap(p->p.getProcesses().stream())
.forEach(proc1 -> projects.values()
.stream()
.flatMap(p->p.getProcesses().stream())
.forEach(proc2 -> {/*do something with proc1 and proc2*/});

关于java - 比较 Java 中列表映射中的每个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54759945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com