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.
3. Which of this interface is not a part of Java's collection framework? Explanation: SortedList is not a part of collection framework.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With