我正在寻找在方法minimumlistPerValueOld中转换为minimumlistPerValueNewgetOptimizedTreeNodeResample(TreeNodeResample in, List<Integer> listSampleRateFinal)

/*I need to find the list that by adding it to the nested list,
  the minimum number of operations required*/

  private static TreeNodeResample getOptimizedTreeNodeResample(TreeNodeResample in, List<Integer> listSampleRateFinal) {
    TreeNodeResample out = new TreeNodeResample(null);

    listSampleRateFinal.forEach(sampleRateFinal -> {
      List<List<NodeResample>> nestedListPerValue = getFilteredNestedListsValue(in, sampleRateFinal);
      Long lastMinimum = Long.MAX_VALUE;
      List<NodeResample> minimumlistPerValue = null;

      for (List<NodeResample> listPerValue : nestedListPerValue) {

        Long accumulator = addCalc(out.getNestedListsValue(), listPerValue)
            .stream()
            .map(nodeResample -> (long) nodeResample.getNumResampleOperations())
            .mapToLong(Long::longValue).sum();

        if (accumulator < lastMinimum) {
          lastMinimum = accumulator;
          minimumlistPerValue = listPerValue;
        }
      }

      out.addListValue(minimumlistPerValue);
    });
    return out;
  }


我相信我需要映射listPerValue,因为您可以看到listPerValue是类型List<NodeResample>

listPerValue ->  {
    TreeNodeResample temp = new TreeNodeResample(null);
    temp.setNestedListsValue(out.getNestedListsValue());
    temp.addListValue(listPerValue);
    return temp.getListNodes();// return List<TreeNodeResample> type
}


或映射(到相同的对象类型)

listPerValue ->  {
    TreeNodeResample temp = new TreeNodeResample(null);
    temp.setNestedListsValue(out.getNestedListsValue());
    temp.addListValue(listPerValue);

    List<NodeResample> childsValues = temp.getListNodes()
    .stream()
    .map(node -> node.getValue())
    .collect(Collectors.toList());
    return childsValues;// return List<NodeResample> type

}


完整的TreeNodeResample类:

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

public class TreeNodeResample {

  TreeNodeResample parent;
  List<TreeNodeResample> children;
  NodeResample value;

  public TreeNodeResample(TreeNodeResample parent) {
    this.parent = parent;
    children = new ArrayList<>();
  }

  public TreeNodeResample(TreeNodeResample parent, NodeResample value) {
    this.parent = parent;
    children = new ArrayList<>();
    this.value = value;
  }

  public void addChild(TreeNodeResample node) {
    if (node != null && node.getValue() != null) {//REVIEW (node.getValue() != null) is needed?
      if (children.stream().noneMatch(child -> Objects.equals(child.getValue(), node.getValue()))) {
        children.add(node);
      }
    }
  }

  public TreeNodeResample getParent() {
    return parent;
  }

  public void cleanChildren() {
    children = new ArrayList<>();
  }

  public int getChildrenCount() {
    return children.size();
  }

  public TreeNodeResample getChildrenAt(int position) {
    if (children.size() > position && position > -1) {
      return children.get(position);
    }
    return null;
  }

  public List<TreeNodeResample> getChildren() {
    return children;
  }

  public NodeResample getValue() {
    return value;
  }

  public boolean isLeaf() {
    return (children.isEmpty());
  }

  public List<TreeNodeResample> getLeafs() {
    return getLeafs(this);
  }

  public void addListValue(List<NodeResample> listValue) {
    addListValue(this, listValue);
  }

  public TreeNodeResample getNode(NodeResample value) {
    return getNode(this, value);
  }

  public List<NodeResample> getListValues() {
    return getListNodes().stream().map(node -> node.getValue()).collect(Collectors.toList());
  }

  public List<TreeNodeResample> getListNodes() {
    List<TreeNodeResample> listNodes = new ArrayList<>();
    getListNodes(this, listNodes);
    return listNodes;
  }

  public List<List<NodeResample>> getNestedListsValue() {
    return getNestedListsValue(this);
  }

  public void setNestedListsValue(List<List<NodeResample>> nestedListsValue) {
    setNestedListsValue(this, nestedListsValue);
  }

  public List<List<NodeResample>> getFilteredNestedListsValue(int sampleRateTarget) {
    return getFilteredNestedListsValue(this, sampleRateTarget);
  }

  public TreeNodeResample getOptimizedTreeNodeResample(List<Integer> listSampleRateFinal) {
    return getOptimizedTreeNodeResample(this, listSampleRateFinal);
  }

  public static void addListValue(TreeNodeResample parent, List<NodeResample> listValue) {
    if (listValue != null) {
      TreeNodeResample node = parent;
      for (NodeResample child : listValue) {
        node = getNode(node, child);
      }
    }
  }

  public static TreeNodeResample getNode(TreeNodeResample parent, NodeResample value) {
    if (parent != null) {//REVIEW (value != null) is needed?
      TreeNodeResample node = parent.getChildren().stream()
          .filter(child -> child != null)
          .filter(child -> Objects.equals(child.getValue(), value))
          .findAny().orElse(null);
      if (node != null) {
        return node;
      }
      node = new TreeNodeResample(parent, value);
      parent.addChild(node);
      return node;
    } else {
      return null;
    }
  }

  public static List<TreeNodeResample> getListNodes(TreeNodeResample parent) {
    List<TreeNodeResample> listNodes = new ArrayList<>();
    getListNodes(parent, listNodes);
    return listNodes;
  }

  public static void getListNodes(TreeNodeResample parent, List<TreeNodeResample> listNodes) {
    if (parent != null) {
      listNodes.add(parent);
      parent.getChildren().forEach(child -> getListNodes(child, listNodes));
    }
  }

  public static List<List<NodeResample>> getNestedListsValue(TreeNodeResample parent) {
    List<TreeNodeResample> listLeafs = getLeafs(parent);
    List<List<NodeResample>> nestedListsValues = listLeafs.stream()
        .map(leaf -> getParentsListValue(leaf))
        .peek(listNodeResample -> {
          //System.out.println(Arrays.toString(listNodeResample.toArray()) + System.lineSeparator() + System.lineSeparator());
        })
        .collect(Collectors.toList());
    return nestedListsValues;
  }

  public static void setNestedListsValue(TreeNodeResample parent, List<List<NodeResample>> nestedListsValue) {
    parent.cleanChildren();
    nestedListsValue.stream()
        .forEachOrdered(listValue -> {
          addListValue(parent, listValue);
        });
  }

  public static List<NodeResample> getParentsListValue(TreeNodeResample leaf) {
    List<NodeResample> listValue = new ArrayList<>();
    if (leaf != null) {
      listValue.add(leaf.getValue());
      TreeNodeResample node = leaf.getParent();
      while (node != null && node.getValue() != null) {
        listValue.add(0, node.getValue());
        node = node.getParent();
      }
    }
    return listValue;
  }

  public static List<List<NodeResample>> getFilteredNestedListsValue(TreeNodeResample parent, int sampleRateTarget) {
    List<TreeNodeResample> listNodes = getListNodes(parent)
        .stream()
        .filter(treeNodeResample -> treeNodeResample.getValue() != null)
        .filter(treeNodeResample -> treeNodeResample.getValue().getSampleRateTarget() == sampleRateTarget)
        .collect(Collectors.toList());

    List<List<NodeResample>> nestedListsValues = listNodes.stream()
        .map(node -> getParentsListValue(node))
        .collect(Collectors.toList());
    return nestedListsValues;
  }

  private static TreeNodeResample getOptimizedTreeNodeResample(TreeNodeResample in, List<Integer> listSampleRateFinal) {
    TreeNodeResample out = new TreeNodeResample(null);

    listSampleRateFinal.forEach(sampleRateFinal -> {
      List<List<NodeResample>> nestedListPerValue = getFilteredNestedListsValue(in, sampleRateFinal);
      Long lastMinimum = Long.MAX_VALUE;
      List<NodeResample> minimumlistPerValue = null;

      for (List<NodeResample> listPerValue : nestedListPerValue) {

        Long accumulator = addCalc(out.getNestedListsValue(), listPerValue)
            .stream()
            .map(nodeResample -> (long) nodeResample.getNumResampleOperations())
            .mapToLong(Long::longValue).sum();

        if (accumulator < lastMinimum) {
          lastMinimum = accumulator;
          minimumlistPerValue = listPerValue;
        }
      }

      out.addListValue(minimumlistPerValue);
    });
    return out;
  }

  private static List<NodeResample> addCalc(List<List<NodeResample>> nestednestedListValue, List<NodeResample> listPerValue) {
    TreeNodeResample temp = new TreeNodeResample(null);
    temp.setNestedListsValue(nestednestedListValue);
    temp.addListValue(listPerValue);
    return temp.getListNodes().stream()
        .map(node -> node.getValue())
        .filter(nodeResample -> nodeResample != null)
        .collect(Collectors.toList());
  }

  public static List<TreeNodeResample> getLeafs(TreeNodeResample parent) {
    List<TreeNodeResample> listLeafs = new ArrayList<>();
    getLeafs(parent, listLeafs);
    return listLeafs;
  }

  private static void getLeafs(TreeNodeResample parent, List<TreeNodeResample> listLeafs) {
    if (parent != null && listLeafs != null) {
      if (parent.isLeaf()) {
        listLeafs.add(parent);
      } else {
        parent.getChildren().forEach(child -> getLeafs(child, listLeafs));
      }
    }
  }

  @Override
  public String toString() {
    return "TreeNodeResample{" + "value=" + value + '}';
  }

  public static void tempPrintNested(List<List<NodeResample>> nestedListNodeResample) {
    System.out.println("  List<List<NodeResample>> nestedListNodeResample = Arrays.asList(");
    for (int o = 0; o < nestedListNodeResample.size(); o++) {
      List<NodeResample> listNodeResample = nestedListNodeResample.get(o);
      System.out.println("    Arrays.asList(");

      for (int i = 0; i < listNodeResample.size(); i++) {
        NodeResample nodeResample = listNodeResample.get(i);
        if (nodeResample != null) {
          System.out.print("      " + nodeResample.getCreator());
          if (i < listNodeResample.size() - 1) {
            System.out.println(",");
          } else {
            System.out.println("\n    )");
          }
        }
      }

      if (o < nestedListNodeResample.size() - 1) {
        System.out.println("    ,");
      }
    }
    System.out.println("  );");
  }

}


另一个NodeResample

public class NodeResample {

  private int incrementL;
  private int decrementM;
  private int sampleRateSource;
  private int sampleRateTarget;
  private double maxPassFreq;
  private Integer filterSize;
  private Integer numResampleOperations;

  public NodeResample(int incrementL, int decrementM, int sampleRateSource, int sampleRateTarget, double maxPassFreq, Integer filterSize, Integer numResampleOperations) {
    this.incrementL = incrementL;
    this.decrementM = decrementM;
    this.sampleRateSource = sampleRateSource;
    this.sampleRateTarget = sampleRateTarget;
    this.maxPassFreq = maxPassFreq;
    this.filterSize = filterSize;
    this.numResampleOperations = numResampleOperations;
  }

  public int getIncrementL() {
    return incrementL;
  }

  public void setIncrementL(int incrementL) {
    this.incrementL = incrementL;
  }

  public int getDecrementM() {
    return decrementM;
  }

  public void setDecrementM(int decrementM) {
    this.decrementM = decrementM;
  }

  public int getSampleRateSource() {
    return sampleRateSource;
  }

  public void setSampleRateSource(int sampleRateSource) {
    this.sampleRateSource = sampleRateSource;
  }

  public int getSampleRateTarget() {
    return sampleRateTarget;
  }

  public void setSampleRateTarget(int sampleRateTarget) {
    this.sampleRateTarget = sampleRateTarget;
  }

  public double getMaxPassFreq() {
    return maxPassFreq;
  }

  public void setMaxPassFreq(double maxPassFreq) {
    this.maxPassFreq = maxPassFreq;
  }

  public Integer getFilterSize() {
    return filterSize;
  }

  public void setFilterSize(Integer filterSize) {
    this.filterSize = filterSize;
  }

  public Integer getNumResampleOperations() {
    return numResampleOperations;
  }

  public void setNumResampleOperations(Integer numResampleOperations) {
    this.numResampleOperations = numResampleOperations;
  }

  @Override
  public String toString() {
    return "NodeResample{" + "L=" + incrementL + ", M=" + decrementM
        + ", Source=" + sampleRateSource + ", Target=" + sampleRateTarget
        + ", filterSize=" + filterSize + ", numResampleOperations=" + numResampleOperations
        + "}   ";
  }

  public String getCreator() {
    return "new NodeResample(" + incrementL + "," + decrementM + "," + sampleRateSource + "," + sampleRateTarget + "," + "0.0" + "," + filterSize + "," + numResampleOperations
        + ")";
  }

  @Override
  public int hashCode() {
    int hash = 3;
    return hash;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    }
    final NodeResample other = (NodeResample) obj;
    if (this.incrementL != other.incrementL) {
      return false;
    }
    if (this.decrementM != other.decrementM) {
      return false;
    }
    if (this.sampleRateSource != other.sampleRateSource) {
      return false;
    }
    if (this.sampleRateTarget != other.sampleRateTarget) {
      return false;
    }
    if (!Objects.equals(this.filterSize, other.filterSize)) {
      return false;
    }
    return true;
  }

}


我想使用Java Streams进行翻译

但是,我感到沮丧...

  List<NodeResample> minimumlistPerValueNew = nestedListPerValue.stream()
      .min(
          Comparator.comparingLong(map(listPerValue -> {
            TreeNodeResample temp = new TreeNodeResample(null);
            temp.setNestedListsValue(out.getNestedListsValue());
            temp.addListValue(listPerValue);
            return temp.getListNodes();
          })
              //.map(node -> node::getValue)
              .filter(nodeResample -> nodeResample != null)
              .mapToLong(NodeResample::getNumResampleOperations).sum())
      )
      .orElse(Collections.emptyList());


恢复情况

public CustomObject wrapperMethod(List<CustomObject> listCustomObjects) {
  Long lastMinimum = Long.MAX_VALUE;
  CustomObject minCustomObject;
  for (CustomObject customObject : listCustomObjects) {
    Long returnedValue = anyMethodReturningLong(customObject);
    if (returnedValue < lastMinimum) {
      lastMinimum = returnedValue;
      minCustomObject = customObject;
    }
  }
  return minCustomObject;
}

最佳答案

您也可以尝试Stream.reduce()解决您的问题。

我从您的问题中了解到的是,存在一个nestedList,并且要从那些内部列表中返回与您的最低标准匹配的列表。
例如,假设我们有这样的[[1,2,4],[5,6,7,8,9,10],[11,12]]嵌套列表,现在您要返回最小list.size()的列表。试试下面的代码:

import java.util.*;
import java.util.stream.*;
public class Main
{
   public static void main(String[] args) {
    List<List<Integer>> nestedList = Arrays.asList(
      Arrays.asList(1,2,4),
      Arrays.asList(5,6,7,8,9,2,5),
      Arrays.asList(10,11,12,13,0));
      Optional<List<Integer>> oplist = nestedList.stream()
      .reduce((list1, list2) -> list1.size() < list2.size() ? list1 : list2);
      oplist.ifPresent(System.out::println);
}
}


输出:[1,2,4]

希望您能从方法中得到启发。现在,您可以尝试在reduce方法中进行比较检查,并检查它是否适合您。

恢复状态的解决方案

//Using .reduce() method

Optional<CustomObject> minCustomObjectList = listCustomObjects.stream()
            .reduce((lastMinimum, returnedValue) -> anyMethodReturningLong(returnedValue) < anyMethodReturningLong(lastMinimum) ? returnedValue : lastMinimum);
minCustomObjectList.ifPresent(e -> System.out.println("Minimum Object using reduce method "+e.toString()));

//Using .min() method

CustomObject minCustomObject = listCustomObjects.stream()
            .min(Comparator.comparingLong(e -> anyMethodReturningLong(e))).get();
System.out.printf("Minimum Object using min method"+minCustomObject.toString());

10-02 15:11