Java Reference
In-Depth Information
an array the number of elements must be known in advance, while the number of
elements in a collection is not defined
a priori
.
Filling a set with two strings:
Collection items
#
new
Vector();
items.add(
new
String("first"));
items.add(
new
String("second"));
Using an array the same population code can be written as follows:
String [] items
#
new
String[2];
items[0]
#
new
String("first");
items[1]
#
new
String("first");
The use of arrays poses several problems: the number of elements must be
known in advance, an index must be used to address elements.
Another important interface is
Map
. It represents the association of a set of keys
to a set of values. The main methods of interface
Map
are
void put(Object key,
Object value)
and
Object get(Object key)
. The former stores an association between
the key and the value, the latter retrieves the value associated with a key (or null
if no value is present).
4.6.3
Implementation
We will have a method (
buildDecisionTree()
) that implements the main fea-
tures of the algorithm, and several auxiliary methods that implement the
details. The root node of the decision tree is computed according to
Algorithm 4.2. Since the algorithm is generic and does not require accessing
any extra information in addition to its arguments, we decide to implement
is using static methods. The method
buildDecisionTree
is as close an imple-
mentation of the original algorithm as possible.
The method receives two parameters; the first is a map that associates
each item with its category; the second is a map that associates the names of
the features with their types. The collection of items can be obtained getting
the key set from the map
items
(using the method
keySet()
).
//Input: a training set of items T, a set of attributes A
//Output: a decision tree (the root of it)
private static
Node buildDecisionTree
(Map items, Map features){
// 0) if the attribute set is empty or the items
// belong to a single class
if
(features.size()
##
0
||
information(items)
##
0.0){
// a) create a leaf node labelled according to the class
// of the item
return new
Node(findCategory(items));
}
// 1) select the "best" split attribute s in A
String splitFeature
#
selectSplit(items,features);
// 2) create a node n with label s.name