6.5 Using The Collections FrameworkHomepage  « Java6 Certification « 6.5 Using The Collections Framework

In our final lesson within this section we look at array and list manipulation using the java.util package, usage of the java.util.Comparator and java.lang.Comparable interfaces for sorting collections and the effect of natural ordering of primitive wrapper classes and java.lang.String on sorting.

Lets take a look at the points outlined at the Oracle Website for this part of the certification.

  • Section 6: Collections / Generics

    • Use capabilities in the java.util package to write code to manipulate a list by sorting, performing a binary search, or converting the list to an array. Use capabilities in the java.util package to write code to manipulate an array by sorting, performing a binary search, or converting the array to a list. Use the java.util.Comparator and java.lang.Comparable interfaces to affect the sorting of lists and arrays. Furthermore, recognize the effect of the "natural ordering" of primitive wrapper classes and java.lang.String on sorting.

java.util.Arrays Classgo to top of page Top

The java.util.Arrays class gives us methods to search, compare, fill, get a hashcode, sort and give a readable string representation of arrays. All the methods accept Object and most are overloaded to accept a primitive type, appropriate to the method in question. The class also includes a static factory that allows arrays to be viewed as lists.

See java.util.Arrays for example code.

java.util.Collections Classgo to top of page Top

The java.util.Collections class static methods give us ways to search, typesafe, empty, synchronize, immute, sort and several other ways to work with our collections. As a reminder collections only work with reference variables, so if you are using primitive variables then these need to be boxed before adding to a collection, or use an array instead.

See java.util.Collections for example code.

Sorting Collectionsgo to top of page Top

In our final look at collection we look at sorting our collections using the Comparable and Comparator interfaces. We have already seen examples of sorted collections when we looked at the TreeSet and TreeMap classes in the Java section. We used some simple code examples to show how these collection types worked and were sorted in natural order according to their types. Maybe what wasn't apparent at the time was that their was no problem with ordering these collections as we were using String and Integer objects implement the Comparable interface and its only method compareTo(). We also used the Collections.sort() method in the last lesson to sort a list, this also used a String object.

But what if we create a TreeSet or TreeMap collection or try to use the Collections.sort() method to sort a custom class? To answer this question we will use the Contractor class we created in the Overriding equals() section of the The Object Superclass lesson. The Contractor class was tested to ensure that the overrides of equals() and hashCode() worked correctly. When creating your own collections you will always want to override these methods if you want meaningful, efficient collections.


java.lang.Comparable<T> Interfacego to top of page Top

Classes can be sorted by using the java.lang.Comparable interface. We need to implement the Comparable interface and its only method compareTo() within our classes. The compareTo() method returns a negative integer, zero, or a positive integer dependant upon this object being less than, equal to, or greater than the specified object.

See the java.lang.Comparable interface for example code.


java.util.Comparator<T> Interfacego to top of page Top

The second way to sort collections is by using the java.util.Comparator interface. The Comparable interface works fine but you have to change the class you want sorted and is a 'one-trick pony' because you can only sort the collection in one way. But what about sorting classes we can't modify or we want to sort in different ways. Luckily for us Java also has the java.util.Comparator<T> interface, which allows us to sort a class without changing the code within that class, which also means we can have multiple sort sequences as well. All we need to do is create a class that implements the java.util.Comparator<T> interface and its compare() method.

See the java.util.Comparator<T> interface for example code.


Related Java6 Tutorials

Collections/Generics - Utilities
Collections/Generics - Sorting Collections


<<  6.3-6.4 Generic Types                    7: Fundamentals  >>

go to home page Java6 Tutor Homepage go to home page Top