gpt4 book ai didi

java - MapTreeStructure 导出为 map

转载 作者:行者123 更新时间:2023-11-30 08:16:27 24 4
gpt4 key购买 nike

以下代码旨在将树数据结构导出为Map<String, String>以便以后更容易操作。但有趣的是toString()方法工作完美,但是 toMap()方法得到了一个失踪的 parent A, child B。有人有什么想法吗?

public static void main(String[] args) {

MutableTree<String> tree = new MappedTreeStructure<String>();
tree.add("A", "B");
tree.add("A", "C");
tree.add("C", "D");
tree.add("E", "F");
System.out.println(tree);

Map<String, String> myMap = tree.toMap();
if (myMap != null) {
for (Map.Entry<String, String> entry : myMap.entrySet()) {
System.out.println("parent: " + entry.getKey() + ", child: "
+ entry.getValue());
}
}

}

private final Map<N, N> nodeParent = new HashMap<N, N>();
private final LinkedHashSet<N> nodeList = new LinkedHashSet<N>();

@Override
public boolean add(N parent, N node) {

boolean added = nodeList.add(node);
nodeList.add(parent);
if (added) {
nodeParent.put(node, parent);
}
return added;
}

@Override
public boolean remove(N node, boolean cascade) {
if (!nodeList.contains(node)) {
return false;
}
if (cascade) {
for (N child : getChildren(node)) {
remove(child, true);
}
} else {
for (N child : getChildren(node)) {
nodeParent.remove(child);
}
}
nodeList.remove(node);
return true;
}

@Override
public List<N> getRoots() {
return getChildren(null);
}

@Override
public N getParent(N node) {
return nodeParent.get(node);
}

@Override
public List<N> getChildren(N node) {
List<N> children = new LinkedList<N>();
for (N n : nodeList) {
N parent = nodeParent.get(n);
if (node == null && parent == null) {
children.add(n);
} else if (node != null && parent != null && parent.equals(node)) {
children.add(n);
}
}
return children;
}

@Override
public String toString() {
StringBuilder builder = new StringBuilder();
dumpNodeStructure(builder, null, "- ");
return builder.toString();
}

@Override
public Map<String, String> toMap() {
Map<String, String> map = new HashMap<String, String>();
dumpNodeToMap(map, null);
return map;
}

private void dumpNodeToMap(Map<String, String> map, N node) {
if (node != null) {
map.put((String) getParent(node), node.toString());
}
for (N child : getChildren(node)) {
dumpNodeToMap(map, child);
}
}

private void dumpNodeStructure(StringBuilder builder, N node, String prefix) {

if (node != null) {

builder.append(prefix);
builder.append(node.toString());
builder.append('\n');
prefix = " " + prefix;
}

for (N child : getChildren(node)) {
dumpNodeStructure(builder, child, prefix);
}

}

控制台输出如下:

- A
- B
- C
- D
- E
- F

parent: null, child: E
parent: A, child: C
parent: C, child: D
parent: E, child: F

作为引用,这些是正在使用的两个接口(interface)类:

public interface MutableTree <N extends Serializable> extends Tree<N> {
public boolean add (N parent, N node);
public boolean remove (N node, boolean cascade);
Map<String, String> toMap();
}

public interface Tree <N extends Serializable> extends Serializable {
public List<N> getRoots ();
public N getParent (N node);
public List<N> getChildren (N node);
}

最佳答案

您的toMap方法返回 a Map<String, String>其键是父元素名称,其值是单个子元素。这意味着当键为“A”时,只能存储一个子元素,并且将其设置为找到的最后一个子元素,在本例中为“C”,覆盖指向“A”的条目“B”。

而是你的 toMap方法需要返回 Map<String, List<String>>它从每个父节点(例如“A”)映射到 List子元素,例如“B”和“C”。显然,如果 List 就可以了。仅包含一个子元素,但如果有多个子元素,它必须是一个列表。

创建 List 的常用模式项目而不是单个项目如下所示:

String parentNode = getParent(node).toString();
List<String> childElements = null;
if(map.contains(parentNode) {
// List of child elements already exists, so get it from the Map.
childElements = map.get(parentNode);
} else {
// List of child elements does not yet exist, so create a new List
// and add it to the Map.
childElements = new ArrayList<>();
map.put(parentNode, childElements);
}
childElements.add(node.toString());

关于java - MapTreeStructure 导出为 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29590449/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com