英文:
why in Java8 the TreeNode subclass in HashMap extends LinkedHashMap.Entry instead of directly extending HashMap's Node subclass?
问题
实际上,一个"treenode"是红黑树中的一个节点,需要一个"parent"字段、一个"left"字段和一个"right"字段来构建一棵树。但我并没有看到它必须具有LinkedHashMap.Entry的某些属性的必要性。
有人知道原因吗?
英文:
Actually a treenode is a node of a red-black tree, and needs a parent field, a left field and a right field to form a tree. but I don't see any necessity for it to have some attributes of an LinkedHashMap.Entry.
Is there anyone who knows the reason?
答案1
得分: 1
可能是因为在HashMap中拥有单独的TreeNode和在LinkedHashMap中拥有LinkedTreeNode所带来的收益较小?
或者是因为LinkedHashMap需要一个LinkedTreeNode,而这个节点需要从两个不同的类继承?
假设HashMap有一个名为HashMap.TreeNode的类,该类仅从HashMap.Node继承。
LinkedHashMap需要一个类似的LinkedHashMap.LinkedTreeNode类,该类需要从LinkedHashMap.Entry继承(LinkedHashMap.Entry从HashMap.Node继承),但还需要从HashMap.TreeNode继承,因为这两个类的交织方式。
这将要求从两个类继承,而Java明确禁止这样做。
英文:
Maybe because the gain from having separate TreeNode in HashMap and LinkedTreeNode in LinkedHashMap is marginal?
Or because LinkedHashMap needs a LinkedTreeNode that would need to extend from to separate classes?
Let's assume that HashMap had a HashMap.TreeNode class that extends solely from HashMap.Node.
LinkedHashMap needs a similar class LinkedHashMap.LinkedTreeNode that needs to extend from LinkedHashMap.Entry (which in turn extends from HashMap.Node), but would also need to extend from HashMap.TreeNode because of the way the two classes are interwoven.
That would require extending from two classes, which Java explicitly prohibits.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论