
The Java Collection API was introduced in Java 1.2 and is a really nice API for handling dynamic arrays, sets, lists etc. The classes for the Collection API can be found in the java.util package. There is no built-in equivalent to the Collection API in LotusScript.
When coming from to Java from LotusScript the Collection API may be a little overwhelming to get cracking on. To kind of ease into it lets look at how to use the basic parts of the API for do kind of the same stuff in Java as you do in LotusScript.
Note: All the collection classes of the Collection API return references of type java.lang.Object which means that references oftentimes needs to be cast back to the desired type.
Dynamic arrays
In LotusScript you can use the Redim and Redim Preserve keywords to change the dimensions of an array at runtime. This isn’t possible in Java. Instead you can use an ArrayList which is an array-like structure that automatically resizes. If you are coming from Java in Notes/Domino 5.x the ArrayList is like a Vector only a little faster.
LotusScript
Dim my_array() As String
Dim i as Integer
For i=0 To 10
Redim Preserve my_array(i)
my_array(i) = "Index: " & i
Next
Java
import java.util.*;
ArrayList l = new ArrayList();
for (int i=0; i<11; i++) {
l.add("Index: " + i);
}
As you can see there is no need to resize the ArrayList in the for-loop.
Lists
LotusScript has the concept of a List which is a key/value datastructure. In Java this kind of collection is called a Map and the most used type is the HashMap. For people coming from Java in Notes/Domino 5.x a HashMap is basically the same as a Hashtable only a wee bit faster.
LotusScript
Dim my_list List As String
my_list("one") = "1"
my_list("two") = "2"
Java
import java.util.*;
HashMap my_map = new HashMap();
my_map.put("one", "1");
my_map.put("two", "2");
Looping an array
All classes of the Collection API implementing the List/Set interface has an iterator() method that returns an Iterator object you can use to loop the collection.
LotusScript
Dim some_array(10) As String
Dim i As Integer
For i=0 To UBound(some_array)
Print some_array(i)
Next
Java
import java.util.*;
ArrayList l = new ArrayList();
Iterator ite = l.iterator();
while (ite.hasNext()) {
System.out.println(ite.next());
}
...or the slightly more condensed version I prefer...
ArrayList l = new ArrayList();
for (Iterator ite = l.iterator(); ite.hasNext(); ) {
System.out.println(ite.next());
}
Looping a List
All classes of the Collection API implementing the Map interface has an keySet() method that returns a Set containing the keys used in the Map. From that Set you use the iterator() method to obtain an Iterator for looping.
LotusScript
Dim my_list List As String
my_list("one") = "1"
my_list("two") = "2"
Forall v In my_list
Print Listtag(v) + " = " + v
End Forall
Java
import java.util.*;
HashMap my_map = new HashMap();
for (Iterator ite=my_map.keySet().iterator(); ite.hasNext(); ) {
Object key = ite.next();
System.out.println(key + " = " + my_map.get(key));
}
As you can see in the Java example the caveat here is to use the my_map.keySet() method to get a Set containing the keys in the Map. From the Set we can get an Iterator that we use to loop the keys of the Map.