为什么Java Collection Framework不包含Tree和Graph [英] Why Java Collection Framework doesn't contain Tree and Graph

查看:209
本文介绍了为什么Java Collection Framework不包含Tree和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 是由Red-Black Tree底层实现的。但是, TreeSet 不是树,而是 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 的子类型。

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.

除了参与设计集合API的人,没有人能给出明确的答案,但这里有两个有根据的猜测...

No one can give a definitive answer except the people that took part in designing the collection API, but here are two educated guesses...


  1. A Collection 表示对象集合。它们可以是有序的或无序的,允许重复或不重复,对迭代顺序给予保证或不等等。但没有一个超出了作为对象集合的范围。

  1. A Collection represents "a collection of objects". They can be ordered or unordered, allow duplicates or not, give guarantees about iteration order or not and so on. But none of them go beyond the scope of being "a collection of objects".

地图有键和值,但在概念上,它们与列表没有很大的不同。它们只是由对象而不是整数索引。

Map has keys and values, but conceptually they are not very different from a list. They are just indexed by objects rather than integers.

树或图形不仅仅是一个集合。在数学中,它通常表示为一对:( V E )其中 顶点和边。

A tree or a graph is more than a collection. In mathematics it's often expressed as a pair: (V, E) where V and E represents a collection of vertices and edges respectively. In other words, it's a collection plus a relation over the objects in the collection.

不包括<$> 的原因是 c $ c> Pair 类在Java API中是因为左边 / 右边第一 / 第二不表示对象表示的任何含义。对于一个复杂的数字,或者<$ c,创建一个包含 real / 虚拟 $ c> firstName / lastName 为全名。

The reason for not including a Pair class in the Java API is because fields such as left/right or first/second doesn't convey any meaning of what the object represents. It's much clearer to create a new class with fields such as real/imaginary for a complex number, or firstName/lastName for a full name.

Person 类,每个人都有一些朋友(形成一个图形),然后放一个 List< Person>朋友字段,或创建 FriendRelation 类。它传递比图方法更多的信息,例如 getNeighborsOf(V vertex) vertexSet() addEdge(V vertex1,V vertex2),...

If you have a Person class and each person has some friends (which form a graph), then put a List<Person> friends field in the person class, or create a FriendRelation class. That conveys more information than graph methods such as getNeighborsOf(V vertex), vertexSet(), addEdge(V vertex1, V vertex2), ...

这篇关于为什么Java Collection Framework不包含Tree和Graph的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆