Java Reference
In-Depth Information
Over time, developers of high-quality calculators have adapted to the standard
algebraic notation rather than forcing its users to learn a new notation. However,
those users who have made the effort to learn RPN tend to be fanatic proponents,
and to this day, some Hewlett-Packard calculator models still support it.
16.7 Using Tree Sets and Tree Maps
Both the HashSet and the TreeSet classes implement the Set interface. Thus, if
you need a set of objects, you have a choice.
If you have a good hash function for your objects, then hashing is usually faster than
tree-based algorithms. But the balanced trees used in the TreeSet class can
guarantee reasonable performance, whereas the HashSet is entirely at the mercy of
the hash function.
The TreeSet class uses a form of balanced binary tree that guarantees that
adding and removing an element takes O(log(n)) time.
If you don't want to define a hash function, then a tree set is an attractive option. Tree
sets have another advantage: The iterators visit elements in sorted order rather than
the completely random order given by the hash codes.
To use a TreeSet , your objects must belong to a class that implements the
Comparable interface or you must supply a Comparator object. That is the same
Search WWH ::




Custom Search