Set Tree Map Comparable Comparator Def The abstract
Set, Tree. Map, Comparable, Comparator
Def: The abstract data type set is a structure that holds objects and satifies ARC: Objects can be added. Objects can be removed. It can be determined if an object is contained in the set. As in the mathematical notion of a set, no duplicate values are permitted in a set. See java. util. set interface.
What is the primary difference between the adt List and the adt Set? There is no ordering in a set. That is, a class that implements the Set interface does not contain a method get(index). The java. util package contains two classes for implementing binary search trees: Tree. Set and Tree. Map
Tree. Set Java. util. Tree. Set is a class that implements the interface Set. Some of the commonly used methods in Tree. Set are: boolean add(Object obj); boolean remove(Object obj); boolean contains(Object obj); int size(); Iterator iterator(); Object[] to. Array();
• When Tree. Set. remove( ) and Tree. Set. add( ) the binary search tree ordering property is preserved. The Tree. Set iterator performs inorder traversals of the tree, delivering values in ascending order. • This class guarantees log (n) time cost for the basic operation remove(), add() and contains(). • Tree. Set contains a default constructor (with no arguments) to create an empty tree that will be sorted by the natural order of objects that are comparable. • Tree. Set contains a constructor with one argument, Comparator c, to create an empty tree that will be sorted by the compare( ) method specified in the Comparator object, c, passed.
What is the difference between Comparable and Comparator?
Comparable is an interface that contains one required method in any class that implements it: compare. To(Object o ). This method must return a negative integer, zero, or a positive integer when this object is less than, equal to, or greater than the specified object, o. The implementor must also ensure that the relation is transitive: (x. compare. To(y)>0 && y. compare. To(z)>0) implies x. compare. To(z)>0. It is strongly recommended, but not strictly required that (x. compare. To(y)==0) == (x. equals(y)). Example: public int compare. To(Object o) { return ((City)o. get. Pop( ) - get. Pop( ); }
Note: Defining compare. To gives one way to compare objects of your class, allowing sorting into ascending order by such library methods as Arrays. sort. However, for more general sorting, such as in descending or or alphabetically, another technique must be used: implementation of the interface Comparator.
• Comparator requires two methods of its implementer: compare( ) and equals( ). • Usually the equals( ) method does not need to be coded, because you can rely on the equals( ) method inherited from Object. Example of a class that implements Comparator: public class Comparator. For. Cities implements Comparator{ public int compare (Object o 1, Object o 2){ String n 1 = ((City) o 1). get. Name(); String n 2 = ((City) o 2). get. Name(); return n 1. compare. To. Ignore. Case(name 2); } }
• To use a comparator object, pass it to constructors or methods that take a Comparator argument, that is any class that implements the interface Comparator. Example code fragment: Comparator. For. Cities cn = new Comparator. For. Cities( ); City [ ] capitals; … Arrays. sort (capitals, cn); … int result = cn. compare(city 1, city 2);
Def: A Map is an adt that associates keys with their values and finds a value for a given key. Commonly used methods in java. util. Map are: • Object put (Object key, Object value); //adds key and associated value to the map, returning any value previously associated with key or null. • Object get (Object key); //returns value associated with key or null. • Boolean contains. Key (Object key); //returns true if key is associated with some value, otherwise returns false. • Int size( ); //returns the number of associated key-value pairs in the map. • Set key. Set( ); //returns the set of keys in the map.
• Java. util. Tree. Map implements java. util. Map. • Tree. Map implements Map as a binary search tree ordered by keys and contains a default constructor, which requires the keys to be comparable, and a one argument (Comparator) constructor. • Tree. Map does not contain an iterator method, but key. Set( ) may be used to get the set of keys and then iterate over them. Example: Tree. Map map = new Tree. Map( ); Some. Type key; Other. Type value; Set keys = map. key. Set( ); Iterator iter = keys. iterator( ); while (itr. has. Next( )) { key = (Some. Type)itr. next( ); value = (Other. Type)map. get(key); …}
How is a Tree. Set different from a Tree. Map? • Tree. Map is more general than Tree. Set. • Although both implement binary search trees, Tree. Set values are compared to each other, while in Tree. Map no ordering is assumed for the values and the tree is arranged according to the order of the keys. • Tree. Set (according to the Java API) is backed by an instance of Tree. Map, which means it has a Tree. Map field in it that does all the work. • It is often easier to use a Tree. Map
- Slides: 13