Java TreeSet example of how to sort students by age and name

  • 2020-11-03 22:12:00
  • OfStack

This article illustrates the Java TreeSet method to sort students by age and name. To share for your reference, the details are as follows:


import java.util.*;
class Treeset
{
 public static void main(String[] args)
 {
  TreeSet t = new TreeSet();
  t.add(new student("a1",15));
  t.add(new student("a2",15));
  t.add(new student("a1",15));
  t.add(new student("a3",16));
  t.add(new student("a3",18));
  for(Iterator it = t.iterator();it.hasNext();)
  {
   student tt = (student)it.next();// Compulsory conversion to student type 
   sop(tt.getName()+","+tt.getAge());
  }
 }
 public static void sop(Object obj)
 {
  System.out.println(obj);
 }
}
class student implements Comparable// Interfaces allow students to be comparative 
{
 private String name;
 private int age;
 student(String name,int age)
 {
  this.name = name;
  this.age = age;
 }
 public int compareTo(Object obj)
 {
  if(!(obj instanceof student))
   throw new RuntimeException(" Not the students ");
  student t = (student)obj;
  if(this.age > t.age)
   return 1;
  if(this.age==t.age)
   return this.name.compareTo(t.name);// If the age is the same, rank the names in comparison 
  return -1;
 }
 public String getName()
 {
  return name;
 }
 public int getAge()
 {
  return age;
 }
}

compareTo

int compareTo(T o)

Compares the order of this object to the specified object. Returns a negative, zero, or positive integer if the object is less than, equal to, or greater than the specified object.

The implementation class must ensure that sgn(x.compareTo (y)) == -sgn (y.compareTo (x)) exists for all x and y. (Which means if y.compareTo(x) Throws an exception, then x.compareTo(y) An exception is also thrown.

The implementation class must also ensure that the relationship is transitive :(x.compareTo (y) > 0 & & y.compareTo(z) > 0) means ES37en. compareTo(z) > 0.

Finally, the implementer must ensure that x.compareTo (y)==0 means that for all z, there is sgn(x.compareTo (z)) == sgn(y.compareTo (z)). It is highly recommended (x. compareTo(y)==0) == (x. equals(y)), but it is not strictly required. In general, any class that implements the Comparable interface and violates this condition should clearly state this fact. The recommendation states: "Note: this class has a natural order not identical to equals."

In the previous description, the symbol sgn(expression) Specifies the signum mathematical function, which returns one of the values -1, 0, or 1, depending on whether the value of expression is negative, zero, or positive.

Parameters:

o - The object to compare.

Returns:

A negative, zero, or positive integer, depending on whether the object is less than, equal to, or greater than the specified object.

Throws:

ClassCastException - If the type of the specified object does not allow it to be compared with this object.

For more information about java algorithm, please refer to Java Data Structure and Algorithm Tutorial, Java DOM Node Operation Skills Summary, Java File and Directory Operation Skills Summary and Java Cache Operation Skills Summary.

I hope this article has been helpful in java programming.


Related articles: