Transitioning into OOP: Manage elements more efficiently with the Java Collections Framework

The Java Collections Framework adds a standardized API for representing groups of elements to the Core Java APIs. This introduction to OOP explores some of the Framework's features and demonstrates how you can use its sorting capabilities.

In our previous article, "Transitioning into OOP: Abstract data types in Java," we talked about how an abstract data type (ADT) presents a software construct that hides its implementation from the developer. This follows the object-oriented programming principle of encapsulation by allowing a developer to use an instance of an ADT without knowing the internal details of its operations. We also discussed some of the classes provided by the Collections Framework of the Java 2 software development kit and explained how they can be used to build powerful abstract data types. In this article, we will present additional classes and features of the Collections Framework and look at its sorting capabilities.

In the Java Collections Framework, a set is a collection of elements that models a mathematical set containing no pair of equal elements. The Set interface extends the Collection interface. This means that you can add objects to the set, remove objects from the set, retrieve an iterator for the set, and so forth. The Set interface adds two methods, each named toArray, that convert a set to an array of objects.

A SortedSet is a set that provides an iterator that will traverse the elements in the set in ascending order. The elements in the set are sorted according to their natural ordering or by a comparator provided at construction time.

A comparator is an interface that defines a comparison function and an equals function, which allow an object that implements it to handle the duties of comparing and determining equality as the situation determines. Comparators are passed to sort methods to control the sorting procedures on a collection of elements.

Unlike Set, the Map interface does not inherit from Collection; instead, it provides an interface with methods that use keys to put and get values, similar to java.util.Hashtable.

A map is an object that contains a list of key/value pairs. A map cannot contain duplicate keys, and each key can map to one value only. A map exposes methods to retrieve a set of keys, a collection of values, and a set of mappings.

There are basically two mechanisms for sorting using the Collections Framework:
  • ·        You can sort any object that implements the List interface using either of two static methods of the Collections class. One method takes a List parameter that implements the Comparable interface. The second method takes a List parameter and a Comparator parameter and sorts the elements of the List using the Comparator object.
  • ·        You can add an implementation of the Comparable interface to your Collection class. This adds a compareTo method to your class, which returns the result of subtracting the second parameter from the first parameter. You then pass your Collection object to an object that implements the Comparator interface.

The example in Listing A demonstrates an implementation of the Comparator interface for sorting a collection of MySortedMapComparator objects.

A SortedMap is a map that provides functionality to maintain its elements in ascending order. The elements are sorted according to the natural ordering of the keys or by a comparator provided at construction time, as the example in Listing B demonstrates.

Sorting it all out
The Java Collections Framework adds a consistent and standardized API for representing groups of sorted and unsorted elements to the Core Java APIs. Since the APIs of the Collections Framework are consistent, once you have learned one part of the Framework, you will know many of the concepts that are used throughout it. This makes the learning curve very short. In our next article, we will begin to explore the input/output system of the Java platform.

Have you wanted to learn OOP?
Do you plan to retool by learning an object-oriented language? How will you leverage Java in your career development? Send us an e-mail with your suggestions and experiences or post a comment below.


Editor's Picks