Monday, March 9, 2009

Collections

This post is a bit of a non-sequitur. The thing is I posted a theory question and the reply took me back to a theory trail, which I had previously omitted.

The first odd this about this trail is that it has a by line, and none of the others do. What does that mean? I think it means it is esoteric - that it was added late by someone with a special interest in the subject. So what does it say?

I quote: "This chapter describes the Java Collections Framework. Here you will learn what collections are and how they can make your job easier and programs better." That sounds good. I posted the question because I had written some clumsy code in VB, and rather than just copying it out in Java, I wanted to use a different approach, and do the job better.

The second paragraph of the introduction says: "If you've used the Java programming language — or just about any other programming language — you're already familiar with collections." That seems a strange thing to put. I've used many languages to quite productive effect without ever having used "collections". And if I had, why would I be reading this chapter?

It's funny how particular words are used by many programming languages but are given different meanings in each one. The Mac used to have a product called HyperCard, which could be automated with a language called HyperTalk, which in turn was was loosely based on AppleTalk. One of the linguistic curiosities I liked about it was that what VB calls a variable and Java calls a field it called a container. I liked the idea of a variable being a container of a value, like a jug contains a quantity of milk.

Anyway, container is given a different meaning here: "A collection — sometimes called a container — is ... an object that groups multiple elements into a single unit". That sounds a bit like an array to me, but I won't complain, I'll read on.

We are next introduced to a Collections Framework, and we are told: "A collections framework is a unified architecture for representing and manipulating collections". We are also told: "Apart from the Java Collections Framework, the best-known examples of collections frameworks are the C++ Standard Template Library (STL) and Smalltalk's collection hierarchy". So it's not every language that uses collections, as was implied above, and it sounds like an esoteric area of those that do.

The first main lesson is called Interfaces. In previous lessons the Interface was a described as a variation on the class theme, and I raved on about how academic it all was from my perspective. Here we are told "The core collection interfaces encapsulate different types of collections" and these can be sets, lists, queues and so on. From the reply to my post it seems I an interested in the list, but I'll get to it in sequence.

There follow some definitions, including a better definition of a collection.

  • A collection represents a group of objects known as its elements.
  • The set is a collection that cannot contain duplicate elements, like a poker hand.
  • The list is an ordered collection (sometimes called a sequence). Lists can contain duplicate elements. The user of a List generally has precise control over where in the list each element is inserted and can access elements by their integer index (position).
  • The queue is a collection used to hold multiple elements prior to processing.
  • A sorted set is a Set that maintains its elements in ascending order.

Now I've been advised to use a list, but I this the sorted set might suit me better. I'll read on.

No comments: