3 回答

TA貢獻1848條經驗 獲得超2個贊
如果嵌套數據的結構是不變的,您可以使用帶有屬性的普通類。
如果結構是動態的,我會使用Maps,接口并忽略實現。
關于使用自定義樹結構,如果可以使用類,那就更好了。如果您使用Maps,我會從 a 開始,HashMap如果您發現這是一個問題,您可以Map稍后將其替換為其他內容。

TA貢獻1993條經驗 獲得超6個贊
您可以為此用例實現 Trie。迭代復合鍵并返回數據(如果找到)。
類定義:
public class TrieNode {
private HashMap<String, TrieNode> children;
private Data data;
private boolean isLeaf;
// ...
}
查找查詢將如下所示:
public Data find(List<String> compositeKey) {
TrieNode current = root;
for (String key: compositeKey) {
TrieNode node = current.getChildren().get(key);
if (node == null) {
return null;
}
current = node;
}
if(current.isLeaf()) {
return current.getData();
} else {
return null;
}
}
插入將如下所示:
public void insert(List<String> compositeKey, Data data) {
TrieNode current = root;
for (String key: compositeKey) {
current = current.getChildren()
.computeIfAbsent(key, c -> new TrieNode());
}
current.setLeaf(true);
current.setData(data);
}

TA貢獻1803條經驗 獲得超6個贊
顯然你必須使用類似樹的數據結構。這是它的示例代碼。
高級代碼思想
class Entity{
// declare you attributes and below two properties
List<Entity> children;
boolean isleafNode;// for parent node its 'false' and for leaf node it will 'true'
}
添加回答
舉報