我有一个嵌套的NodeResampleII类列表
class NodeResampleII {
private int incrementL;
private int decrementM;
private int sampleRateSource;
private int sampleRateTarget;
public NodeResampleII(int incrementL, int decrementM, int sampleRateSource, int sampleRateTarget) {
this.incrementL = incrementL;
this.decrementM = decrementM;
this.sampleRateSource = sampleRateSource;
this.sampleRateTarget = sampleRateTarget;
}
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;
}
@Override
public String toString() {
return "NodeResample{" + "L=" + incrementL + ", M=" + decrementM
+ ", Source=" + sampleRateSource + ", Target=" + sampleRateTarget
+ "}";
}
}
我使用了JTree
,插入了NodeResampleII
的嵌套列表,然后进行了计数和演算摘要,以避免在完全相同的位置(重合)重复节点(项目)完全从头开始)。
当我在JTree
中插入多个列表时,我会排除重复的计数和总和。
这是我的代码在做什么
public class OptimizationResampleII {
public static void main(String[] args) {
final JTree tree = new JTree();
tree.setRootVisible(false);
removeAllTreeNodes(tree);
List<List<NodeResampleII>> nestedList = filler();
fillTreeNestedListNodeResampleII(tree, nestedList);
expandAllNodes(tree);
final JTextArea textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
nestedList.forEach(list -> {
textArea.append(list.stream().map(n -> n.toString()).collect(Collectors.joining(",\t")) + System.lineSeparator() + System.lineSeparator());
System.out.println(list.stream().map(n -> n.toString()).collect(Collectors.joining(",\t")) + System.lineSeparator() + System.lineSeparator());
});
JSplitPane splitPane = new JSplitPane();
splitPane.setOrientation(JSplitPane.VERTICAL_SPLIT);
splitPane.setTopComponent(new JScrollPane(tree));
splitPane.setBottomComponent(new JScrollPane(textArea));
splitPane.setDividerLocation(350);
JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
panel.add(splitPane);
JFrame frame = new JFrame("Optimization NodeResample II");
frame.setContentPane(panel);
frame.pack();
frame.setSize(new Dimension(860, 700));
frame.setVisible(true);
}
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);
}
private static void expandAllNodes(JTree tree) {
expandAllNodes(tree, 0, tree.getRowCount());
}
private static void expandAllNodes(JTree tree, int ini, int rows) {
try {
if (tree != null) {
for (int i = ini; i < rows; ++i) {
tree.expandRow(i);
}
if (tree.getRowCount() != rows) {
expandAllNodes(tree, rows, tree.getRowCount());
}
}
} catch (ArrayIndexOutOfBoundsException ex) {
} catch (Exception ex) {
}
}
private static void fillTreeNestedListNodeResampleII(JTree tree, List<List<NodeResampleII>> nestedListNodeResample) {
nestedListNodeResample.stream()
.forEachOrdered(listNodeResample -> {
addNodeResampleToTree(tree, listNodeResample);
});
}
private static void addNodeResampleToTree(JTree tree, List<NodeResampleII> listNodeResample) {
try {
DefaultTreeModel treeModel = ((DefaultTreeModel) tree.getModel());
DefaultMutableTreeNode rootTreeNode = (DefaultMutableTreeNode) treeModel.getRoot();
DefaultMutableTreeNode nodeResampleTreeNode = rootTreeNode;
if (listNodeResample != null) {
for (NodeResampleII nodeResample : listNodeResample) {
nodeResampleTreeNode = getDefaultMutableTreeNode(nodeResampleTreeNode, nodeResample, true);
}
}
treeModel.reload(rootTreeNode);
} catch (Exception e) {
throw e;
}
}
private static DefaultMutableTreeNode getDefaultMutableTreeNode(DefaultMutableTreeNode parent, NodeResampleII 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 List<List<NodeResampleII>> filler() {
List<List<NodeResampleII>> nestedListNodeResample = Arrays.asList(
Arrays.asList(
new NodeResampleII(7, 5, 3200, 4480),
new NodeResampleII(3, 4, 4480, 3360),
new NodeResampleII(1, 2, 3360, 1680),
new NodeResampleII(1, 2, 1680, 840),
new NodeResampleII(1, 2, 840, 420),
new NodeResampleII(1, 2, 420, 210),
new NodeResampleII(4, 3, 210, 280),
new NodeResampleII(5, 7, 280, 200)
),
Arrays.asList(
new NodeResampleII(7, 5, 3200, 4480),
new NodeResampleII(3, 4, 4480, 3360),
new NodeResampleII(1, 2, 3360, 1680),
new NodeResampleII(1, 2, 1680, 840),
new NodeResampleII(1, 2, 840, 420),
new NodeResampleII(1, 2, 420, 210),
new NodeResampleII(4, 3, 210, 280),
new NodeResampleII(4, 7, 280, 160)
),
Arrays.asList(
new NodeResampleII(7, 5, 3200, 4480),
new NodeResampleII(3, 4, 4480, 3360),
new NodeResampleII(1, 2, 3360, 1680),
new NodeResampleII(1, 2, 1680, 840),
new NodeResampleII(1, 2, 840, 420),
new NodeResampleII(1, 2, 420, 210),
new NodeResampleII(6, 7, 210, 180)
),
Arrays.asList(
new NodeResampleII(7, 5, 3200, 4480),
new NodeResampleII(3, 4, 4480, 3360),
new NodeResampleII(1, 2, 3360, 1680),
new NodeResampleII(1, 2, 1680, 840),
new NodeResampleII(1, 2, 840, 420),
new NodeResampleII(1, 2, 420, 210),
new NodeResampleII(5, 7, 210, 150)
),
Arrays.asList(
new NodeResampleII(3, 2, 3200, 4800),
new NodeResampleII(3, 5, 4800, 2880),
new NodeResampleII(1, 2, 2880, 1440),
new NodeResampleII(1, 2, 1440, 720)
),
Arrays.asList(
new NodeResampleII(3, 2, 3200, 4800),
new NodeResampleII(3, 5, 4800, 2880),
new NodeResampleII(1, 2, 2880, 1440),
new NodeResampleII(1, 2, 1440, 720)
)
);
return nestedListNodeResample;
}
}
从我的图片中可以看到,JTree
中每个位置只有一个 Item / Node(NodeResampleII
)。在列表中会重复出现。
我重复了下面的列表两次,但是,在“树”中仅表示一次。
是否有一种无需使用JTree即可获得这些项目列表的方法?
我想省去已经实现的方法(与JTree / DefaultTreeModel / DefaultMutableTreeNode
相关)。 (getDefaultMutableTreeNode, addNodeResampleToTree, fillTreeNestedListNodeResampleII
)