简体   繁体   English

如何使用 Java lambda 解决嵌套循环

[英]How to solve nested loops using a Java lambda

I have an object which uses 2 nested loops to find matching processes based on a pid.我有一个对象,它使用 2 个嵌套循环来查找基于 pid 的匹配进程。 The goal is to collect all the processes which occur in tuple.目标是收集元组中发生的所有进程。 A tuple is used to store 2 processes that have the same processName but different process pids.元组用于存储具有相同 processName 但不同进程 pid 的 2 个进程。 Processes are always spawned in pairs.进程总是成对产生的。

Here is a snippet of the class.这是课程的片段。

class Process{

  private String name;
  private String pid;
  private String processName;

   public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof Process)) return false;
    Process that = (Process) o;
    return Objects.equals(pid , that.pid);
    etc...
 }

   class Tuple {
      Process parent;
      Process child;
      Tuple(Process first, Process second){
      }
   }

Here is how I process the list of orders:以下是我处理订单列表的方式:

  List<Process> processes..
  List<Tuple> tuples = new ArrayList<>();
  for( Process p0 : processes){
     for(Process p1 : processes){
        if(p0.processName.equals(p1.processName)){
          p1.pid != p0.pid;
          Tuple tp= new Tuple(p0, p1);
          tuples.add(tp);
        }
     } 

  }

How can I use a Lambda function to accomplish this in Java 12?如何在 Java 12 中使用 Lambda 函数来完成此操作?

When you ask for "lambda function to accomplish this" , I believe your actually asking for how to do it using streams .当您要求“实现此目的的 lambda 函数”时,我相信您实际上是在询问如何使用来实现。

I'll assume that Tuple is a separate top-level class, or a static nested class, not an inner class, as it currently shows in the question.我会假设Tuple是一个单独的顶级类,或者一个static嵌套类,而不是一个内部类,正如它目前在问题中显示的那样。

List<Tuple> tuples = processes.stream()
        .flatMap(p0 -> processes.stream()
                .filter(p1 -> p0.getPid() != p1.getPid()
                           && p0.getProcessName().equals(p1.getProcessName()))
                .map(p1 -> new Tuple(p0, p1)))
        .collect(Collectors.toList());

For better performance, I would however suggest doing it like this:为了获得更好的性能,我建议这样做:

List<Tuple> tuples2 = processes.stream()
        .collect(Collectors.groupingBy(Process::getProcessName))
        .values().stream()
        .filter(list -> list.size() > 1)
        .flatMap(list -> list.stream()
                .flatMap(p0 -> list.stream()
                        .filter(p1 -> p0.getPid() != p1.getPid())
                        .map(p1 -> new Tuple(p0, p1))
                )
        )
        .collect(Collectors.toList());

I hope you realize that all tuples will have mirrors, ie for every tuple(a,b) you're going to also have a tuple(b,a) .我希望您意识到所有元组都会有镜像,即对于每个tuple(a,b)您也将有一个tuple(b,a) To prevent that, change the p0.getPid() != p1.getPid() conditions to p0.getPid() < p1.getPid() .为防止出现这种情况,请将p0.getPid() != p1.getPid()条件更改为p0.getPid() < p1.getPid()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM