05.0 Java Notes‎ > ‎

Java: Collection Framework

Notes based on the session at the ACSE Conference November 2005 -- York University

Component based Programming using the Java Collection Framework

Any programming component (class) consists of two major concepts:
    - data (a.k.a. the fields) or the attributes of the components.
    - algorithms (a.k.a. the methods) or the actions that the components can perform.

Each component:
    - belongs to a package.
    - the package must be imported into the program.
    - is either utility or non-utility
        - utility components are static and can be invoked on the class.
        - non-utility components are not static and must be instantiated to be used.
    - is either concrete or not
        - a concrete component can be used an created in a program directly
        - a component that is not concrete (abstract or interface) must be extended by a new class to be implemented.

Component based programming does not implement new classes, it uses existing classes.

    
When using the Java collection framework the successful programmer must:
    - focus on WHAT is needed to be done, not HOW to do it(the programming detail).
    - solve problem using the available collection components.
    - read the API to find the ready made components.
    - build applications by assembling the various java components.   
    - understand variables and their data types.
    - understand programming structures (sequence, selection and repetition (loops)).

When using the Java Collection framework the programmer learns:
    - to communicate using the API. (this will help when creating their own API)
    - the concept of encapsulation.
    - to separate the concerns of WHAT to do vs. HOW to do it (in detail)
    - that the emphasis of computer science is on the specifications.

There are three interfaces that the Java Collection Framework uses:  Lists, Sets and Maps
Each of these interfaces implement two structures from arrays, hash tables or trees.

When using a collection
    - declare using the interface, not the class.
    - always specify the type of element that you intend to store in the collection.

List 
    - duplicates are allowed
    - positional order is significant (lists are sequences)
        ArrayList
            slow when you add to the front of the list.
        LinkedList 
            use only when your applications tend to add or remove at element 0 (the beginning of the list). 

Set
    - does not allow duplicates.
    - positional order is NOT significant .

        HashSet
            faster than a tree -- you will notice a difference when there are about 1 000 000 elements in the set.
            not sorted.

        TreeSet
            sorted.

Map
    - data stored in a map is always paired
    - Map(key,Value)
        - the key must be unique (i.e. all key values create a Set).
        - the value can be any not-primitive data type.

        HashMap
            - faster than a tree -- you will notice a difference when there are about 1 000 000 elements in the set.
            - not sorted.   

        TreeMap
           - sorted.
Comments