场合
我有一个包含数据对象的无序列表,每个对象都有一个对其父对象的引用。该列表应该通过引用父类进行排序,最终列表应该按顺序排列,就好像它是一棵树一样。孩子们应该按名字命名。
数据对象:
/**
* Object with a parent relationship
*/
public static class Node {
Node parent;
String text;
int level = -1;
public Node(Node parent, String text) {
this.parent = parent;
this.text = text;
}
public String toString() {
return text;
}
}
示例树:
/**
* Create example data
* @return
*/
private static List<Node> createExampleList() {
List<Node> list = new ArrayList<>();
Node root = new Node(null, "root");
Node a = new Node(root, "a");
Node b = new Node(root, "b");
Node c = new Node(root, "c");
Node a1 = new Node(a, "a1");
Node a2 = new Node(a, "a2");
Node a3 = new Node(a, "a3");
Node b1 = new Node(b, "b1");
Node b2 = new Node(b, "b2");
Node b3 = new Node(b, "b3");
Node c1 = new Node(c, "c1");
Node c2 = new Node(c, "c2");
Node c3 = new Node(c, "c3");
Node b11 = new Node(b1, "b11");
Node b12 = new Node(b1, "b12");
Node b13 = new Node(b1, "b13");
list.add(root);
list.add(a);
list.add(b);
list.add(c);
list.add(a1);
list.add(a2);
list.add(a3);
list.add(b1);
list.add(b11);
list.add(b12);
list.add(b13);
list.add(b2);
list.add(b3);
list.add(c1);
list.add(c2);
list.add(c3);
return list;
}
问题
我目前的解决方案是内存很重。到目前为止我的算法:
DefaultMutableTreeNode
并将其放入地图DefaultMutableTreeNode
表示,并将节点添加到父级DefaultMutableTreeNode
的枚举机制遍历树并迭代地将每个项添加到列表中问题
有没有人知道更快,更有效的内存方式?
代码
这是我到目前为止的代码:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.tree.DefaultMutableTreeNode;
/**
* Sort a list of parent-child related items. The resulting list should be ordered as if the list were a tree.
* The algorithm converts the list to an intermediary tree using {@link DefaultMutableTreeNode} as a wrapper.
* That way you can create the final list depending on your needs, i. e. preorder enumeration, breadth first enumeration, etc.
*/
public class Main {
public static void main(String[] args) {
// create example list
List<Node> nodes = createExampleList();
// shuffle
Collections.shuffle(nodes);
logList("shuffled list", nodes);
// convert list -> tree and tree -> sorted list
DefaultMutableTreeNode root = createTree(nodes);
List<Node> sortedList = createList(root);
logList("sorted list", sortedList);
System.exit(0);
}
private static DefaultMutableTreeNode createTree(List<Node> nodes) {
// we want the final sublists sorted by name
Collections.sort(nodes, new Comparator<Node>() {
@Override
public int compare(Node o1, Node o2) {
return o1.text.compareTo(o2.text);
}
});
// create DefaultMutableTreeNode objects for every node
Map<Node, DefaultMutableTreeNode> treeNodeMap = new HashMap<>();
for (Node node : nodes) {
treeNodeMap.put(node, new DefaultMutableTreeNode(node));
}
DefaultMutableTreeNode root = null;
// connect the tree nodes depending on their relation
for (Node node : nodes) {
DefaultMutableTreeNode treeNode = treeNodeMap.get(node);
// find root node
if (node.parent == null) {
root = treeNode;
}
// otherwise attach the node to its parent
else {
// get the parent treenode
DefaultMutableTreeNode parentTreeNode = treeNodeMap.get(node.parent);
// attach the current node to its parent
parentTreeNode.add(treeNode);
}
}
return root;
}
private static List<Node> createList(DefaultMutableTreeNode root) {
List<Node> nodes = new ArrayList<>();
// iterate through the tree in preorder, extract the node object and add it to the list. in addition the level is set as meta information, used later for indenting during logging
Enumeration<DefaultMutableTreeNode> enumeration = root.preorderEnumeration();
while (enumeration.hasMoreElements()) {
// get tree node
DefaultMutableTreeNode treeNode = enumeration.nextElement();
// get node from tree node
Node node = (Node) treeNode.getUserObject();
// update the level
node.level = treeNode.getLevel();
// add to list
nodes.add(node);
}
return nodes;
}
/**
* Log the node list
* @param text
* @param list
*/
private static void logList(String text, Collection<Node> list) {
System.out.println();
System.out.println(text);
System.out.println();
for (Node item : list) {
String paddedString = createString( item.level * 2, ' ');
System.out.println( " " + paddedString + item);
}
}
/**
* Create a string filled with {@code length} times the given {@code character}.
* @param length
* @param character
* @return
*/
public static String createString(int length, char character) {
if (length <= 0)
return "";
char[] array = new char[length];
Arrays.fill(array, character);
return new String(array);
}
/**
* Create example data
* @return
*/
private static List<Node> createExampleList() {
List<Node> list = new ArrayList<>();
Node root = new Node(null, "root");
Node a = new Node(root, "a");
Node b = new Node(root, "b");
Node c = new Node(root, "c");
Node a1 = new Node(a, "a1");
Node a2 = new Node(a, "a2");
Node a3 = new Node(a, "a3");
Node b1 = new Node(b, "b1");
Node b2 = new Node(b, "b2");
Node b3 = new Node(b, "b3");
Node c1 = new Node(c, "c1");
Node c2 = new Node(c, "c2");
Node c3 = new Node(c, "c3");
Node b11 = new Node(b1, "b11");
Node b12 = new Node(b1, "b12");
Node b13 = new Node(b1, "b13");
list.add(root);
list.add(a);
list.add(b);
list.add(c);
list.add(a1);
list.add(a2);
list.add(a3);
list.add(b1);
list.add(b11);
list.add(b12);
list.add(b13);
list.add(b2);
list.add(b3);
list.add(c1);
list.add(c2);
list.add(c3);
return list;
}
/**
* Object with a parent relationship
*/
public static class Node {
Node parent;
String text;
int level = -1;
public Node(Node parent, String text) {
this.parent = parent;
this.text = text;
}
public String toString() {
return text;
}
}
}
输出:
shuffled list
b11
a
b13
c2
b1
b3
b
c1
a3
c
b12
a1
b2
c3
a2
root
sorted list
root
a
a1
a2
a3
b
b1
b11
b12
b13
b2
b3
c
c1
c2
c3
答案 0 :(得分:1)
一种可能性是使用流和递归:
public static void main(String[] args) {
// create example list
List<Node> nodes = createExampleList();
// shuffle
Collections.shuffle(nodes);
logList("shuffled list", nodes);
// create tree list
logList("tree list", treeList(nodes));
}
private static List<Node> treeList(final List<Node> nodes) {
return treeAdd(nodes, null, new ArrayList<>(nodes.size()));
}
private static List<Node> treeAdd(List<Node> nodes, Node parent, List<Node> treeList) {
nodes.stream()
.filter(n -> n.parent == parent)
.sorted((n1,n2) -> n1.text.compareTo(n2.text))
.forEach(n -> {
n.level = parent == null ? 0 : parent.level + 1;
treeList.add(n);
treeAdd(nodes, n, treeList);
});
return treeList;
}
请注意,这在性能方面不是非常有效,因为过滤器调用被称为n次并且是O(n)。可以通过预先初始化地图以通过父级查找子项来优化它。