As you might know a TreeSet will always have a sorted order

As you might know, a TreeSet will always have a sorted order of elements. The question is how are they sorted? Integers, doubles and floats are sorted according to their size/magnitude (that is all that they have, after all). String objects are sorted alphabetically. What about custom class objects? How will aTreeSet sort Student objects? The answer is that it will not work, because the compiler has no idea how to compare a student with another student. In order for students to be comparable, a Comparable interface needs to be implemented in the class. Two things are needed, both shown in two code snippets below on the example of ExamPaper. The compareTo method belongs in the interface Comparable. When the method is added, the compiler will know how to compare two objects of the same type. If you want to know a bit more about how the compare To method functions, just read the documentation (or hover over the name of the method in Eclipse). Your taskis to implement a Comparable interface in the Student class, which will compare the students according to their age. Place all students in a TreeSet. Print the set in the end using an Iterator. An example ho wan iterator works is given below:

Solution

public class Student implements Comparable<Student> {

   String name;
   String address;
   int age;

   /**
   * @param name
   * @param address
   * @param age
   */
   public Student(String name, String address, int age) {
       this.name = name;
       this.address = address;
       this.age = age;
   }

   /**
   * @return the name
   */
   public String getName() {
       return name;
   }

   /**
   * @param name
   * the name to set
   */
   public void setName(String name) {
       this.name = name;
   }

   /**
   * @return the address
   */
   public String getAddress() {
       return address;
   }

   /**
   * @param address
   * the address to set
   */
   public void setAddress(String address) {
       this.address = address;
   }

   /**
   * @return the age
   */
   public int getAge() {
       return age;
   }

   /**
   * @param age
   * the age to set
   */
   public void setAge(int age) {
       this.age = age;
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return \"Student [name=\" + name + \", address=\" + address + \", age=\"
               + age + \"]\";
   }

   /*
   * (non-Javadoc)
   *
   * @see java.lang.Comparable#compareTo(java.lang.Object)
   */
   @Override
   public int compareTo(Student obj) {
       if (this.getAge() == obj.getAge())
           return 0;
       else

       if (this.getAge() < obj.getAge())
           return 1;
       else
           return -1;

   }

}


import java.util.Iterator;
import java.util.TreeSet;

public class TestStudent {

   public static void main(String[] args) {

       TreeSet<Student> students = new TreeSet<Student>();
       Student student1 = new Student(\"Srinivas\", \"VSKP\", 24);
       Student student2 = new Student(\"Kumar\", \"HYD\", 23);
       Student student3 = new Student(\"Kishore\", \"BNGLR\", 25);
       Student student4 = new Student(\"Rajesh\", \"CHENNAI\", 45);

       students.add(student1);
       students.add(student2);
       students.add(student3);
       students.add(student4);

       Iterator<Student> iterator = students.iterator();
       while (iterator.hasNext()) {
           Student student = iterator.next();
           System.out.println(student);

       }

   }
}

OUTPUT:

Student [name=Rajesh, address=CHENNAI, age=45]
Student [name=Kishore, address=BNGLR, age=25]
Student [name=Srinivas, address=VSKP, age=24]
Student [name=Kumar, address=HYD, age=23]

 As you might know, a TreeSet will always have a sorted order of elements. The question is how are they sorted? Integers, doubles and floats are sorted accordin
 As you might know, a TreeSet will always have a sorted order of elements. The question is how are they sorted? Integers, doubles and floats are sorted accordin
 As you might know, a TreeSet will always have a sorted order of elements. The question is how are they sorted? Integers, doubles and floats are sorted accordin

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site