Blog

Mapping Java Collections

  |   Blog   |   8 Comments

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.

AUTHOR - Paul Withers

Paul Withers is an IBM Lifetime Champion, has been an OpenNTF Board Member since 2013, has worked with Domino since R4.5, XPages since 2009, co-authored XPages Extension Library and was technical editor for Mastering XPages 2nd Edition. He is one of the developers on OpenNTF Domino API as well as contributor to a variety of other OpenNTF projects. For full bio, see https://paulswithers.github.io/professional/

8 Comments
  • Tim Tripcony | Feb 25, 2013 at 5:28 pm

    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.

  • Paul Withers | Feb 26, 2013 at 8:42 am

    Yes, TreeMap is very useful.

  • Rami Jundi | Feb 26, 2013 at 1:14 pm

    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.

  • Tommy Valand | Feb 28, 2013 at 1:36 pm

    Thanks for the link to the cheat sheet 🙂

  • Daniel Svanström | Mar 4, 2013 at 6:19 pm

    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).

  • Mikkel Flindt Heisterberg | Mar 8, 2013 at 6:26 am

    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 🙂

  • Janeve George | Jan 12, 2018 at 12:02 pm

    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.

    • Paul Withers | Jan 15, 2018 at 12:46 pm

      Thanks, that’s great news. I’ve updated the blog post.

Post A Comment