Re: Java TreeMap Simulator
See the following for a tutorial on how to use Map's
The Map Interface (The Java™ Tutorials > Collections > Interfaces)
A TreeMap is just an implementation of the Map interface explained above, which provides sorting of the Keys. See
TreeMap (Java Platform SE 6)
Re: Java TreeMap Simulator
My confusion is what TreeMap has to do with simulating something. Anyway, I recently learned more about TreeMap. Other examples I had seen typically loaded a key with String and used the add("blahblah") method to enter the values into the TreeMap and the examples worked fine because using a String literal in quotes allocates memory for each add. I initially used a scan in a loop for loading data into TreeMap, but got wierded out when all the keys turned out the same. The solution I found was to load data from the scan into a Vector, which actually holds data as opposed to points to it. Then load the record in Vector into TreeMap. Actually two Vectors, one for <Key> and the other for <Entry>. I suspect that a single Vector with the key as part of the iterator in the Vector would also work ok.
Really cool about TreeMap is that the tree is a self balancing red-black tree (cf. Knuth), which could get to be significant for a big, long term project. Were this not for a class, for small apps I'd say load it all into a Vector because it is simpler and you can arrange things as you like among the elements.
//Tm4I
//
//
// TreeMap value V is a shallow copy. To make a TreeMap really have a payload of V values,
// make a Vector array of the value V. How much you want to bet that the only reason why
// keyword K worked is because the string constants in t.put are different locations ... ?
//
// Ok, now this works. What next?
//
// Class Index will be an improvement. You can put all the mess in what place.
// TreeMap to do the actual work, Vectors containing String for K and int for V
// K loads JList at top, V references particular record in Marc
//
//
import java.util.*;
public class Tm4
{
public static void main(String[] args)
{
Tm4I x0 = new Tm4I(1);
Tm4I x1 = new Tm4I(2);
Tm4I x2 = new Tm4I(3);
Tm4I x3 = new Tm4I(4);
Tm4I x4 = new Tm4I(5);
Tm4I x5 = new Tm4I(6);
Tm4I x6 = new Tm4I(7);
Tm4I x7 = new Tm4I(8);
Tm4I x8 = new Tm4I(9);
Tm4I x9 = new Tm4I(10);
Tm4I y;
TreeMap<String,Tm4I> t = new TreeMap<String,Tm4I>();
t.put("one ",x0);
t.put("two",x1);
t.put("three",x2);
t.put("four",x3);
t.put("five",x4);
t.put("six",x5);
t.put("seven",x6);
t.put("eight",x7);
t.put("nine",x8);
t.put("ten",x9);
System.out.println(t.subMap("one","ten").firstKey( ));
System.out.println(t.firstKey() + "\n\n");
y = t.get("six");
System.out.println("***" + y.getI());
y = t.get("two");
System.out.println("***" + y.getI());
{
// search similar to matchPlus;
String min = "one";
String max = "ten";
String cursor = t.floorKey(min);
do
{
if((cursor != null) && (cursor.compareTo(max) <= 0))
{
System.out.println(cursor);
cursor = t.higherKey(cursor);
}
}
while(cursor.compareTo(max) <= 0);
}
}
}
// Used by Tm4
public class Tm4I
{
public int i;
Tm4I()
{
}
Tm4I(int n)
{
i = n;
}
public int getI()
{
System.out.println(i);
return i;
}
public void setI(int n)
{
i = n;
System.out.println(i);
}
}
Does not do much but run, but I hope it is a help. Like I say, I found using TreeMap a little strange. Good Luck!