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 or you may prefer a direct link to download the PDF from that page.

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.

8 thoughts on “Mapping Java Collections”

  1. That cheatsheet is so handy. The reason TreeMap is one of my favorites is that it’s automatically sorted. So whatever you use as your key set, you can populate the map in whatever sequence you find the content, then when you iterate the map, it’s already sorted it for you. This drastically reduces the need for creating views just to ensure that the data you want to display to the user is sorted correctly.

  2. I have found these classes very useful over the years for mapping data and sorting data. My favorite feature is they all should have an inexpensive sort method since they implement java.util.collections.

    They definitely are a baby step into mixing java into your SSJS as stated. I have also used them in legacy code to sort LotusScript lists also.

  3. The main reason for what you are describing in an ArrayList vs a TreeMap is that the ArrayList is a List in Java and not intended to be used as a key/value implementation. The key is merely an index of the object in the list. The key/value implementation is in Java terms a Map, such as the TreeMap. There are a number of useful maps, such as HashMap, Properties or the TreeMap you mentioned. Out of those only the TreeMap keeps its objects sorted by the key (it implements SortedMap).

  4. Please way out when you are using sorted maps eg SortedMap as the keys needs to be sortable. Actually they need to be comparable meaning implementing the java.lang.Comparable interface which if course String does why they work great in this context. Just watch out when you starting using other types for keys or you’ll get a nasty run time exception 🙂

  5. Paul,

    I am the owner of the website you referenced in this article. First and foremost, thank you for sharing the article and the Java Collections Matrix. I hope it was useful to you.

    The spam ads were a result of some bad decisions I had made. I have removed all those malicious ads and have renovated the website with a brand new look and more articles and categories.

    Feel free to publish the article and the site is safe to be shared. Please get in touch with me in case you have any other concerns regarding the article.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top