比较Java列表中的每个对象

时间:2019-02-19 06:20:53

标签: java performance arraylist hashmap iterator

我有一个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的{​​{1}}之间的字符串距离(使用flow)。我提出了以下建议:

Process

有没有更有效,更优雅的方法来实现这一目标?

3 个答案:

答案 0 :(得分:2)

也许有Stream s:

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*/});

答案 1 :(得分:0)

也许加一点糖:

    for (Project proj1: projects.values()) {
        for (Process proc1: proj1.getProcesses()) {
            for(Project proj2: projects.values()) {
                for(Process proc2: proj2.getProcesses()) {
                    //use JaroWinklerDistance to compare proc1.getFlow() with proc2.getFLow()
                }
            }
        }
    }

答案 2 :(得分:0)

List<Process> allProcesses = projects.values()
    .stream()
    .flatMap(p->p.getProcesses().stream());

for (Process currentProcess : allProcesses) {
    for (Process process : allProcesses) {
        //distance calculation
    }
}