Collections: Dictionaries, Arrays, Lists and Maps in iOS, Windows Store apps and Android


Arrays (or lists) and dictionaries (or maps) are a  common way to store values when programming. This post looks at some of the differing conventions between iOS, Windows and Android.

iOS (Xcode)

NSArray, NSDictionary and NSSet are part of the iOS Collections framework. Arrays and dictionaries in iOS are free to hold any combination of objects, and it isn't necessary to declare types that the arrays can hold. An NSArray and an NSMutableArray are the basic array (or list) classes. The only difference between the two is that an NSArray is immutable and cannot be added to, or subtracted from, once created (except by certain sorting methods) but an NSMutableArray can have objects removed and inserted. (The methods associated with each class reflect this accordingly.)

A basic example of creating an NSArray:

NSArray *arrayWords = [[NSArray alloc] initWithObjects: @"Hello", @"World", @"Have a super day", nil];

or more simply

NSArray *arrayWords = @[@"Hello", @"World", @"Have a super day"];

NSDictionary and NSMutableDictionary have the same relationship as NSArrays and NSMutableArrays but instead of a list of objects contain key/value pairs.

NSDictionary *dictionaryWords = @{@"Hello":@"World", @"Have": @"a super day"};

In addition to the Array and Dictionary classes, there exists NSSet, NSMutableSet and NSCountedSet. The purpose of sets as described by Apple's documentation is:

"You can use sets as an alternative to arrays when the order of elements isn’t important and performance in testing whether an object is contained in the set is a consideration—while arrays are ordered, testing for membership is slower than with sets."

You can create a set like this:

NSSet *setWords = [[NSSet alloc] initWithArray:@[@"Hello", @"World", @"Have a super day"]];

Windows (Visual Studio)

Windows contains five namespaces under the umbrella of System.Collections each providing a comprehensive range of classes. There is only time to consider Array, List and Dictionary here.

The Array class is similar to List, but is created at a fixed size. The fixed size does not prevent objects from being inserted and removed after creation but the length remains constant.

We can create an array like this:

Array array = Array.CreateInstance( typeof(String), 5 );
array.SetValue("Beer",1);
array.SetValue("Chips",3);

Debug.WriteLine(array.GetValue(3));
Debug.WriteLine(array.Length); 

Or like this:

String[] stringArray = { “Beans”, “Pizza”, “Beer”, "Chips", “Milk”};
Array array = stringArray;

Note: In the second example we've created an array using the String class and then passed it to an Array object.

Moving on, a List is created like this:

List<String> itemsList = new List<string>(){ "1", "2", "3", "4", "5" };
itemsList.Add("6");

Meanwhile, a Dictionary is similar to NSDictionary/NSMutableDictionary in its function and is created by casting the classes of object it is expected to contain, for the key and value, within angle brackets.

Dictionary<String, String> dictionary = new Dictionary<String, String>();
dictionary.Add("key", "value");

Windows also has a Stack class where items can be pushed onto the stack and popped off the stack using a last-in-first-out (LIFO) approach – if you wanted to emulate this behaviour with NSMutableArray in iOS you would use the addObject and removeLastObject classes in combination.

As stated above, things don't end there and to really understand the full range of options you should look at all of the Systems.Collection namespaces.

Android (Eclipse)

Under the heading 'Java Collections Framework', Zigurd Mednieks et al. (Programming Android, O'Reilly) clearly outline the different types of interface associated with holding an array of objects - Collection, List, Set, Map, Iterator - and their implementation types - ArrayList, LinkedList, HashSet, HashMap, TreeMap.

They teach us here that 'An ArrayList is a list that is backed by an array. It is quick to index but slow to change size.' and 'A LinkedList is a list that can change size quickly but is slower to index.'

This means that when you want to access objects in an Array quickly but don't often add to the array you should use an ArrayList but if you are adding and subtracting items but rarely querying the contents, you should use a LinkedList.

In addition to this knowledge, it is useful to know that there are a couple of ways in which we can create, for example, an array of strings in Android. The first is to use the String class like this:

String[] myStringArray = {"Cola","Chips","Beer","Bread","Butter","Water"};

The other is to use the ArrayList:

ArrayList<String> arrayList = new ArrayList<String>();

arrayList.add("Cola");
arrayList.add("Chips");
arrayList.add("Beer");
arrayList.add("Bread");
arrayList.add("Butter");
arrayList.add("Water");

And to convert a string array to a List we do something like this:

List<String> list = Arrays.asList(myStringArray);

When it comes to a dictionary, or in Android's case a Map class, the Android Developer documentation encourages the use of the SimpleArrayMap class

SimpleArrayMap<String, String> map = new SimpleArrayMap<String, String>();
map.put("key","value");

And as with the other operating systems there is also a Set interface

Further Reading

How to initialize List object in Java? (StackOverflow)

dotnetperls.com/array (DotNetPerls)

dotnetperls.com/collections (DotNetPerls)

Iterator class (Android, Developer)

Iterators (C# and Visual Basic) (Windows, Developer)



Endorse on Coderwall

Comments