Mapping Java Collections
I’ve been using Java for some time and one of the benefits is that there are a whole host of options for managing collections of objects. In SSJS we have arrays, as we do in LotusScript. LotusScript also gives Lists. SSJS and Java also make use of Vectors, most often for lookup keys, field values or column values. (Note that Vectors are an older Java construct and usually deprecated now.) SSJS also used maps and even newbie XPages developers actually use them, with scoped variables and variables such as session, database and var names in repeat controls. These are all stored in a request scope during the XPage’s lifecycle, scoped to a particular context. For example, session and database are scoped to the whole XPage but stored only during the current request. The var name of a repeat is scoped just to that repeat, which is why it is easily accessible within that repeat, even from custom controls nested in the repeat, but cannot easily be accessed from outside the repeat.
As I moved into Java, I became more familiar with collections. ArrayList and TreeMap were the most commonly used. ArrayLists are like dynamic arrays, holding a specific type of data. TreeMaps are a bit like Lists in LotusScript in that they hold a key and a specific data type. But they are more powerful than Lists because the key can be any data type, whereas LotusScript Lists only accept a key that is a string. So myList(“1″) = “Test” will get overwritten by myList(1) = “New Test”.
But I’ve got frustrated in a number of occasions by not knowing which type of collection to use for what. So in a fit of pique I turned to Google and searched for some kind of cheat sheet of collections. This page gave a very nice one, with a nice matrix of what is available and usage scenarios.
And bear in mind that all these maps can be instantiated in SSJS as well as Java, prefixing with the package name “java.util”, so java.util.TreeSet. Hopefully it will be a useful resource for those starting to step beyond the basic arrays and maps we know from SSJS.