Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Java Collection Framework doesn't contain Tree and Graph

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.

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.

like image 465
卢声远 Shengyuan Lu Avatar asked Feb 12 '11 14:02

卢声远 Shengyuan Lu


People also ask

Which is not part of Java collection framework?

3. Which of this interface is not a part of Java's collection framework? Explanation: SortedList is not a part of collection framework.

Does Java have a tree library?

Java provides two in-built classes, TreeSet and TreeMap, in Java Collection Framework that cater to the needs of the programmer to describe data elements in the aforesaid form.

Why Map is not a part of collection hierarchy?

Because they are of an incompatible type. List, Set and Queue are a collection of similar kind of objects but just values where a Map is a collection of key and value pairs.

Is tree a collection in Java?

TreeSet t = new TreeSet(Collection col); TreeSet(SortedSet): This constructor is used to build a TreeSet object containing all the elements from the given sortedset in which elements will get stored in default natural sorting order.


1 Answers

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.

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: Lists allow duplicates, sets do not

  • index: List values are indexed by integers, map values are indexed by other objects.

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.

like image 170
aioobe Avatar answered Oct 19 '22 10:10

aioobe