Logout

Home Topic 4 Last Next

4.3.10

Describe the characteristics and applications of a collection.

 

Teaching Note:

Characteristics:
- Contains similar elements.

LINK HL extension, recursive thinking.

LINK General principles of computational thinking, connecting computational thinking and program design.

 

Sample Question:

sdfsdfsf

JSR Notes:


Characteristics of Collections

A collection is any programming structure which holds a group of similar elements, and can dynamically change its size. Though note that, technically, if you are talking about the Collection class of Java, they can in fact be collections different types. :

So a collection is a grouping of similar things, which can grow or shrink in size. And by this definition we do not include arrays, since they cannot change in size.


Application of Collections

Collections are used to conveniently work with groups, often very large groups, of data.

Recall that computers are mainly good at two things: complex decision structures, and working with lots of things very quickly. The later needs some way to group these "lots of things", and it is collections, generally, which allow this.

So we find collections used everywhere in IT, whether it be collections of pixels on a screen, or collections of letters on a page, or collections of Student objects in a school database.



Features of Collections

In addition, it makes sense to take a look at a couple of the key features of collections.

Traversal
Collections offer a way to traverse through the data efficiently one way or the other. In the case of Java Collection classes such as ArrayList and LinkedList, it is by using some sort of "getNext()" pointer (an HL topic) or using a for loop to iterate through the whole list.

        ArrayList<Integer> list = new ArrayList<Integer>();
        list.add(3);
        list.add(234);
        list.add(997);
        for(int i = 0; i < list.size(); i++){
            System.out.println(list.get(i));
        }


Collections as OOP objects

Collections, whether arrays or lists, are actually, themselves objects which point to the data they represent. That is to say, the array or list variable is technically the address of where the array content or the list content starts, in memory. A distinct advantage of having this kind of implemented is that the object/address itself can be passed as a parameter. This saves on doubling up the memory required whenever working with a collection in another method.

    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<String>();
        method1(list);
        System.out.println(list.get(0)); //prints Hello
    }
    
    public static void method1(LinkedList<String> arrList){
        arrList.add("Hello");
    }

 

 

Refer to 4.2.2 for a related assessment statement.

 

(The Java util package contains an abstract class called Collection from which specific implementations of it have been made, for example, ArrayList, and LinkedList and TreeSet. And if you really want to see ArrayList and/or LinkedList in operation at this point, you could look ahead to D.4.11

Also, it's worthy of note that Java arrays are not an implementation of the Collection. Arrays are a fundamental structure of Java. Yes, they generally can be seen as collections, but they are not implementation of the Collection class.)