Java Collections Framework

From CSSEMediaWiki
Jump to: navigation, search

Contents

Overview

This section presents a higher level take on the Java Collections Framework (JCF) originally designed by Joshua Bloch while working at Sun Microsystems in 1997. According to Joshua, the JCF was designed using three main ideas:

1) Interfaces that define Collections
2) Implementations that provide either concrete or abstract implementations
3) Algorithms that allow you to manipulate the collections

The main motivation behind the framework was that it was extensible, allowing people to build on it and add to it. This has lead to a number of highly specialised implementations of the framework by different people. For example, the open-source Google Guava Libraries provide implementations of some immutable, unmodifiable and synchronised collection classes. Collections can play an important role not only in program efficiency but also in good OO Design. Using the wrong collections can lead to a maintenance nightmare. Hence, this article aims to equip the readers with the essentials of using collections with emphasis on software design (concepts like Inheritance, Good OO practices, Concepts , Hidden Design Patterns) as compared to a tutorial on the subject. Using the correct collection for a task can make the design highly simplified.

The JCF Design

The high level design of the JCF Components has been given below.

Collections-detail-1.png

The Choice of Collection

The Collections Interface

This section gives a brief introduction/ refresher on the choice of data structure according to the needs of the programmer. It gives a software engineering viewpoint on Data Structures. A more detailed explanation can be found in any 'Data Structures' book.

The main questions which we need to ask before choosing a collection are given below :

1) Are Duplicates allowed ?
2) Is Ordering important?
3) What order do we want to iterate over the collection?
- Insertion order
- Comparator Order
- User Defined Ordering
- No specific order desired

(These points were highlighted by Kevin Bourrillion during his talk on Guava).

Concepts and Hidden Patterns

This section covers the design concepts behind the Java Collections Framework. The aim is to help OO Designers get a better understanding of the framework by underlining the hidden concepts and main design patterns that make up this framework.

Iterators

Inbuilt Iterators are one of the main features of Java Collections which make them a lot more user friendly than say, simple arrays. These can be obtained from the collection.terator() method or used under the hood through the enhanced for-each loop in java. Iterator Pattern has been used to provide this functionality to programmers.

Comparators

Strategy Pattern has been used to allow programmers to extend the Comparable interface to define special comparators. This is done by the use of overriding the compareTo() method. For example, a StringLengthComparator Class can be defined which extends a Comparator<String> and allows string comparison based on length instead of the usual lexicographic comparison.

Guava - The Google Collections Library

Introduction

Features and Benefits

New Collections

Links

Personal tools