使用列表列表

时间:2019-05-11 02:25:37

标签: java list java-8 java-stream jtree

我想实现流程的方法优化,我简化了该流程(如所示的树)(乘法和除法)。

enter image description here

底部和粗体矩形(在线工具不允许我更改颜色)表示每个列表的 N 分子和 D 分母之间的总和。< / p>

现在,我正在用Java创建列表列表,以便计算每个列表(流程)的总成本。

我的第一步是创建一个类...

public class Pair {

  private int num;
  private int den;

  public Pair(int num, int den) {
    this.num = num;
    this.den = den;
  }

  public int getNum() {
    return num;
  }

  public void setNum(int num) {
    this.num = num;
  }

  public int getDen() {
    return den;
  }

  public void setDen(int den) {
    this.den = den;
  }

  @Override
  public String toString() {
    return "Pair{" + "num=" + num + ", den=" + den + '}';
  }
}

第二步是...

公共类TestPair {

 public static void main(String... args) {
   ArrayList<ArrayList<Pair>> listOfListPair = new ArrayList<>();

   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(1, 2)}));
   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(3, 1)}));
   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(2, 1)}));


   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(1, 2)}));
   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(13, 3)}));


   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(1, 2)}));
   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 1)}));
   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(3, 1)}));
   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(5, 1)}));
   listOfListPair.add((ArrayList<Pair>) Arrays.asList(
       new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 7)}));

   javax.swing.JTree myTree = new javax.swing.JTree();
   listOfListPair.stream().forEachOrdered(listPair -> {

     //HOW POPULATE THE myTree?

   });
 }

}

是否有一些递归填充我的树的线索?

1 个答案:

答案 0 :(得分:0)

我的回答几乎没有变化...

enter image description here

public class TestPair {

  public static void main(String... args) {
    List<List<Pair>> listOfListPair = new ArrayList<>();

    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(1, 2)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(3, 1)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(3, 1), new Pair(2, 1)}));

    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(1, 2)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(11, 2), new Pair(13, 3)}));

    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(1, 2)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 1)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(3, 1)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(5, 1)}));
    listOfListPair.add((List<Pair>) Arrays.asList(
        new Pair[]{new Pair(1, 1), new Pair(1, 7), new Pair(2, 1), new Pair(2, 7)}));

    final JTree tree = new JTree();
    tree.setRootVisible(false);
    removeAllTreeNodes(tree);
    listOfListPair.stream().forEachOrdered(listPair -> {
      addPairToTree(tree, listPair);
    });

    JFrame frame = new JFrame("Optimization Pair");
    JScrollPane scrollPane = new JScrollPane();
    scrollPane.setViewportView(tree);
    frame.add("Center", scrollPane);
    frame.pack();
    frame.setSize(new Dimension(600, 600));
    frame.setVisible(true);
  }

  private static void addPairToTree(JTree tree, List<Pair> listPair) {
    try {
      DefaultTreeModel treeModel = ((DefaultTreeModel) tree.getModel());
      DefaultMutableTreeNode rootTreeNode = (DefaultMutableTreeNode) treeModel.getRoot();
      DefaultMutableTreeNode pairTreeNode = rootTreeNode;
      final Pair leafPair = new Pair(1, 1);
      for (Pair pair : listPair) {
        pairTreeNode = getDefaultMutableTreeNode(pairTreeNode, pair, true);
        leafPair.setDen(leafPair.getDen() * ((Pair) pairTreeNode.getUserObject()).getDen());
        leafPair.setNum(leafPair.getNum() * ((Pair) pairTreeNode.getUserObject()).getNum());
      }
      getDefaultMutableTreeNode(pairTreeNode, leafPair, false);
      treeModel.reload(rootTreeNode);
    } catch (Exception e) {
      throw e;
    }
  }

  private static DefaultMutableTreeNode getDefaultMutableTreeNode(DefaultMutableTreeNode parent, Pair newChild, Boolean isLeaf) {
    if (parent != null) {
      DefaultMutableTreeNode child;
      for (int i = 0; i < parent.getChildCount(); i++) {
        child = (DefaultMutableTreeNode) parent.getChildAt(i);
        if (child.toString().equals(newChild.toString())) {
          return child;
        }
      }
      child = new DefaultMutableTreeNode(newChild, isLeaf);
      parent.add(child);
      return child;
    } else {
      return null;
    }
  }

  private static void removeAllTreeNodes(JTree tree) {
    DefaultMutableTreeNode rootTreeNode = (DefaultMutableTreeNode) tree.getModel().getRoot();
    if (rootTreeNode != null) {
      rootTreeNode.removeAllChildren();
    }
    reloadTree(tree);
  }

  private static void reloadTree(JTree tree) {
    DefaultTreeModel treeModel = ((DefaultTreeModel) tree.getModel());
    DefaultMutableTreeNode rootTreeNode = (DefaultMutableTreeNode) treeModel.getRoot();
    treeModel.reload(rootTreeNode);
  }

}