<< Chapter < Page Chapter >> Page >

The String class implements the Comparable interface. Therefore, objects instantiated from the String class have a natural ordering when placed in a collection.

Because the compareTo method of the String class, (which implements the Comparable interface) considers upper-case and lower-case characters to be different, there were no duplicate elements added to thecollection when only the compareTo method was used to compare elements. The six String objects were simply arranged so that the iterator would return references to those objects in sorted order. This produced the output shownbelow:


A TreeSet with a Comparator

The code shown in Listing 4 was used to instantiate a new TreeSet object. A Comparator object's reference was passed to the TreeSet constructor. The Comparator object (instead of the compareTo method) was subsequently used for comparing and controlling the sorting order of the elements in the TreeSet collection.

Listing 4 . A TreeSet with a Comparator.
ref = new TreeSet(new TheComparator());Populator.fillIt(ref);iter = ref.iterator(); while(iter.hasNext()){System.out.print(iter.next() + " "); }//end while loop

The code in Listing 4 was also used to populate the collection, and to display the contents of the collection after it was populated.

Populating the TreeSet collection

As before, the fillIt method shown in Listing 5 was used to populate the collection. The same six names as before were added to the TreeSet collection. However, the result of adding those six names was determined by the behavior of the compare method in the Comparator object used by the TreeSet object for managing the collection. (Three of the names were rejected as duplicates.)

Listing 5 . Populating the TreeSet collection.
public static void fillIt(Collection ref){ ref.add("Joe");ref.add("Bill"); ref.add("Tom");ref.add("JOE"); ref.add("BILL");ref.add("TOM"); }//end fillIt()

Beginning of the Comparator class

The code in Listing 6 shows the beginning of the class from which the Comparator object was instantiated. Note that this class implements the Comparator interface, and therefore defines a concrete version of the method named compare .

Listing 6 . Beginning of the Comparator class.
class TheComparator implements Comparator,Serializable{public int compare(Object o1,Object o2){ if(!(o1 instanceof String))throw new ClassCastException(); if(!(o2 instanceof String))throw new ClassCastException();

Listing 6 doesn't contain the interesting part of this class. The code in Listing 6 simply throws an exception if the compare method receives any incoming parameters of types other than String .

The interesting code

The interesting code in the method named compare is shown in Listing 7 .

Listing 7 . The interesting code in the compare method.
int result = ((String)o1).toUpperCase().compareTo(((String)o2). toUpperCase());return result; }//end compare()

The code in Listing 7 makes use of two methods of the String class to compare the two incoming objects.

Questions & Answers

what is the stm
Brian Reply
is there industrial application of fullrenes. What is the method to prepare fullrene on large scale.?
industrial application...? mmm I think on the medical side as drug carrier, but you should go deeper on your research, I may be wrong
How we are making nano material?
what is a peer
What is meant by 'nano scale'?
What is STMs full form?
scanning tunneling microscope
how nano science is used for hydrophobicity
Do u think that Graphene and Fullrene fiber can be used to make Air Plane body structure the lightest and strongest. Rafiq
what is differents between GO and RGO?
what is simplest way to understand the applications of nano robots used to detect the cancer affected cell of human body.? How this robot is carried to required site of body cell.? what will be the carrier material and how can be detected that correct delivery of drug is done Rafiq
what is Nano technology ?
Bob Reply
write examples of Nano molecule?
The nanotechnology is as new science, to scale nanometric
nanotechnology is the study, desing, synthesis, manipulation and application of materials and functional systems through control of matter at nanoscale
Is there any normative that regulates the use of silver nanoparticles?
Damian Reply
what king of growth are you checking .?
What fields keep nano created devices from performing or assimulating ? Magnetic fields ? Are do they assimilate ?
Stoney Reply
why we need to study biomolecules, molecular biology in nanotechnology?
Adin Reply
yes I'm doing my masters in nanotechnology, we are being studying all these domains as well..
what school?
biomolecules are e building blocks of every organics and inorganic materials.
anyone know any internet site where one can find nanotechnology papers?
Damian Reply
sciencedirect big data base
Introduction about quantum dots in nanotechnology
Praveena Reply
what does nano mean?
Anassong Reply
nano basically means 10^(-9). nanometer is a unit to measure length.
do you think it's worthwhile in the long term to study the effects and possibilities of nanotechnology on viral treatment?
Damian Reply
absolutely yes
how to know photocatalytic properties of tio2 nanoparticles...what to do now
Akash Reply
it is a goid question and i want to know the answer as well
characteristics of micro business
for teaching engĺish at school how nano technology help us
How can I make nanorobot?
Do somebody tell me a best nano engineering book for beginners?
s. Reply
there is no specific books for beginners but there is book called principle of nanotechnology
how can I make nanorobot?
what is fullerene does it is used to make bukky balls
Devang Reply
are you nano engineer ?
fullerene is a bucky ball aka Carbon 60 molecule. It was name by the architect Fuller. He design the geodesic dome. it resembles a soccer ball.
what is the actual application of fullerenes nowadays?
That is a great question Damian. best way to answer that question is to Google it. there are hundreds of applications for buck minister fullerenes, from medical to aerospace. you can also find plenty of research papers that will give you great detail on the potential applications of fullerenes.
how did you get the value of 2000N.What calculations are needed to arrive at it
Smarajit Reply
Privacy Information Security Software Version 1.1a
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get the best Algebra and trigonometry 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?