为什么 Java Collection Framework 不包含 Tree 和 Graph [英] Why Java Collection Framework doesn't contain Tree and Graph
问题描述
我熟悉 Java Collection Framework,它包含基本接口:Collection
和 Map
.我想知道为什么框架不包含作为基本集合的树和图的结构.两者都可以看作是Collection
的子类型.
I am familiar with Java Collection Framework which contains basic interfaces: Collection
and Map
. I am wondering why the Framework doesn't contain structures as Tree and Graph which are basic collections. Both can be regarded as sub types of Collection
.
顺便说一下,我知道TreeSet
是由红黑树底层实现的.但是,TreeSet
不是 Tree 而是 Set
,因此框架中没有真正的 Tree.
By the way, I know TreeSet
is implemented by Red-Black Tree underlying. However, the TreeSet
is not a Tree but a Set
, so there's no real Tree in the framework.
推荐答案
我想知道为什么框架不包含作为基本集合的树和图的结构.两者都可以看作是
Collection
的子类型.
这是个好问题.我认为这可以归结为范围界定.Collections API 提供的类的核心功能是:
This is a good question. I think it simply boils down to scoping. The core features that Collections API provides classes for are:
迭代顺序:列表和排序映射有指定的迭代顺序,大多数集合没有.
iteration order: Lists and sorted maps have specified iteration order, most sets don't.
duplicates:列表允许重复,集合不允许
duplicates: Lists allow duplicates, sets do not
index:列表值由整数索引,地图值由其他对象索引.
index: List values are indexed by integers, map values are indexed by other objects.
这让我们走得很远,我认为 Joshua Bloch 等人认为可以在顶部实现更多功能丰富的集合(需要元素之间的内部关系的图和树、具有多重性的集合、双向映射等)这三个核心功能中的一部分,因此在库中效果更好.
This gets us very far and I assume Joshua Bloch et al argued that more feature rich collections (graphs and trees which require internal relationship between elements, sets with multiplicity, bi-directional maps, ...) can be implemented on top of these three core features, and are thus better off in libraries.
这篇关于为什么 Java Collection Framework 不包含 Tree 和 Graph的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!