<< Chapter < Page Chapter >> Page >
Listing 9 . Display the contents of the TreeSet object.
iter = ref.iterator(); while(iter.hasNext()){System.out.print(iter.next()); }//end while loop

The output produced by this code fragment is shown below.


As you can see, the duplicate elements having the value 4 were eliminated as would be expected for a Set object. In addition, the Comparator was used to accomplish the following contract of a SortedSet object:

"A set that further guarantees that its iterator will traverse the set in ascending element order, sorted according to the natural ordering of itselements (see Comparable ), or by a Comparator provided at sorted set creation time."

In this case, the sorted order was controlled by the Comparator object, and not by the natural ordering of the elements. The natural ordering is controlled by implementation of the Comparable interface, and the elements in this collection did not implement the Comparable interface. Therefore, objects of the class named MyClass do not have a natural order in this program.

However, the code in this version of the Comparator produced an output order that matches the ascending natural order that one might expectfor objects of type MyClass . Future modules will show you how to design the Comparator object to produce different output orders, such as descending order for example.

Run the program

I encourage you to copy the code Listing 2 and paste it into your Java editor. Then compile and execute it.

Run the program and observe the results. Experiment with the code. Make changes, run the program again, and observe the results of your changes. Make certain that youcan explain why your changes behave as they do.


This module has discussed and illustrated the use of the Comparator interface.

The sorting order established by a Comparator may be different or may be the same as the natural ordering for a collection of objects .

A Comparator can be used to establish a sorting order for objects that don't have a natural ordering .

The use of a Comparator is an alternative to the implementation of the Comparable interface.

A TreeSet object instantiated with the benefit of a Comparator object doesn't require the objects in its collection to implement Comparable .

What's next?

In the next module, I will illustrate the use of a Comparator to eliminate the effect of case when sorting String objects.


This section contains a variety of miscellaneous information.

Housekeeping material
  • Module name: Java4090: The Comparator Interface, Part 1
  • File: Java4090.htm
  • Published: 05/07/13

Financial : Although the Connexions site makes it possible for you to download a PDF file for thismodule at no charge, and also makes it possible for you to purchase a pre-printed version of the PDF file, you should beaware that some of the HTML elements in this module may not translate well into PDF.

I also want you to know that, I receive no financial compensation from the Connexions website even if you purchase the PDF version of the module.

In the past, unknown individuals have copied my modules from cnx.org, converted them to Kindle books, and placed them for sale on Amazon.com showing me as the author. Ineither receive compensation for those sales nor do I know who does receive compensation. If you purchase such a book, please beaware that it is a copy of a module that is freely available on cnx.org and that it was made and published withoutmy prior knowledge.

Affiliation : I am a professor of Computer Information Technology at Austin Community College in Austin, TX.


Get the best College algebra course in your pocket!

Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?