Working with collections
Data structures are one of the essential aspects of every piece of software that has ever been written. Any application, from simple command line util to scalable enterprise systems, constantly process various information, that very often require specific grouping and access strategies. This aspect is addressed by arrays and more advanced collection types available both in Java and .NET environments. By using the Javonet framework, users gain ability to easily and effectively work with data structures originating from the other platform.
I'll be working with the following pseudo-service class, which main purpose is to produce and consume various collection types.
Retrieving and handling collections
Collection-type objects retrieved by the Javonet framework, are accessible via the NObject/JObject class instances on the Java side. Therefore working with these objects is really simple, as I'll show in the next few examples.
The following code retrieves an instance of a Set
In the example below, we're using this approach to check the size of the received Set
In contrast to sets, lists and dictionaries do allow its users for selective access to the contained objects. For this purpose, we'll be using the NObject.getIndex(Object index) method, which is only applicable for NObjects representing .NET types allowing for such access. In case you would try to invoke this method on a non-indexed .NET object, Javonet framework will throw a JavonetException, stating that given .NET class does not provide indexed values.
The following two examples present usage of this approach against List
Dictionary:
Passing collections
Sending collection-type objects from Java to .NET is no different from passing any other object. All we are required to do is grab the NObject instance, representing specific .NET collection object and use it in the method call. In the following example, we'll instantiate, populate and send a simple .NET list of strings as an argument to the "UseList" method of our .NET CollectionService object. This should result in printing all of the list's contents to standard output stream.
As I'll be using parametrized versions of .NET collection types, I am assuming you are familiar with the Javonet-way of instantiating such objects. In case you're not, I strongly encourage you to have a look at respective section of Quick Guide, available on the Javonet home page.
Similarly as when retrieving collections from .NET, the approach for sending NObject instances as arguments of method calls, can also be applied for setting .NET object's properties etc.
Here's another example in which we're creating a Dictionary<String,String> object, populate it and pass it as an argument to our .NET CollectionService object's "UseDictionary" method. Exactly as in previous example, we should get contents of our dictionary object in the standard output stream.
Summary
In this article we've focused on working with collections using Javonet framework. We've briefly explained basic techniques of handling collection objects, covering retrieval, enumeration- and index-based access to their contents, as well as instantiation and usage as arguments in the method calls.
Was this article helpful?