Win a copy of Learning Java by Building Android Games this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Explain Working of compareTo() method with respect to this example  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/* WHEN IS COMPARETO METHOD CALLED, WHY DO WE NEED TO PUT ELEMENTS OF TREESET IN TO AN OBJECT ARRAY AND THEN TYPE CAST IT.
*/


import java.util.*;

class Comparable1
{
public static void main(String[] args)
{
TreeSet ts1=new TreeSet();

ts1.add(new Student("Saurabh","Thard",100,0.1));

ts1.add(new Student("Sachin","Tendulkar",200,0.01));

ts1.add(new Student("Prannoy","Roy",300,03.03));

ts1.add(new Student("Barkha","Dutt",400,04.4));

ts1.add(new Student("Vikram","Chandra",500,5.05));

Object [] studArray=ts1.toArray();

Student s;

for(Object obj:studArray)

{
s=(Student) obj;

System.out.printf("NAME = %s %s STUDENT ID =%d GPA=%.1f \n",s.fname(),s.lname(),
s.StudentID(),s.gpa());

}
}
}
class Student implements Comparable
{

String fname;
String lname;
int studentID;
double gpa;

public Student(String fname,String lname,int studentID,double gpa)
{

if(fname==null || lname==null || studentID==0 || gpa==0.0)

{
throw new IllegalArgumentException();
}

this.fname=fname;
this.lname=lname;
this.studentID=studentID;
this.gpa=gpa;
}

public String fname()
{
return fname;
}

public String lname()
{
return lname;
}

public int StudentID()
{
return studentID;
}

public double gpa()
{
return gpa;
}

public int compareTo(Object o) //OVER-RIDING COMPARETO SINCE IT IMPLEMENTS COMPARABLE.
{
double f= gpa- ((Student)o).gpa;

if(f==0.0)
return 0;

else if(f<0.0)
return -1;

else
return 1;
}
}



 
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please always used code tags
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

WHEN IS COMPARETO METHOD CALLED,



its is called every time you add an element to TreeSet. Since treeset by default does the sorting on the basis of comparator implemented for the class. In this case Student is object and the Student class is implementing comparator interface.
 
Sheriff
Posts: 9613
37
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the compareTo method will actually be called only when add method is called on a TreeSet which has any elements. If there are no elements in the TreeSet, then the method will not be called on adding an element .

And as far as the type casting goes, you have to do that only because you didn't use generics. If you use this syntax for your declaration



Then you don't need any type casts and you can use the enhanced for loop like this

  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!