• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please always used code tags
 
Harshit Rastogi
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Yeah, but how did the squirrel get in there? Was it because of the tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic