why in Java8 the TreeNode subclass in HashMap extends LinkedHashMap.Entry instead of directly extending HashMap's Node subclass?

huangapple go评论149阅读模式
英文:

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.EntryHashMap.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.

huangapple
  • 本文由 发表于 2020年4月4日 15:18:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/61024926.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定