12:34 am - Monday May 1, 2017

TreeSet with comparator by user define objects.

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

This tutorial explains how  to implement TreeSet with comparator by user define objects.

To implement your own sorting functionality with TreeSet on user defined objects,

you have to pass Comparator object along with TreeSet constructor call.

The Comparator implementation holds the sorting logic.

Example

import java.util.Comparator;

import java.util.TreeSet;

public class MyCompUserDefine

{

   public static void main(String a[])

{

      TreeSet<Empl> nameComp = new TreeSet<Empl>(new MyNameComp());

       nameComp.add(new Empl("Ram",3000));

       nameComp.add(new Empl("John",6000));

       nameComp.add(new Empl("Crish",2000));

       nameComp.add(new Empl("Tom",2400));

       for(Empl e:nameComp){

           System.out.println(e);

       }

       System.out.println("===========================");

       TreeSet<Empl> salComp = new TreeSet<Empl>(new MySalaryComp());

       salComp.add(new Empl("Ram",3000));

       salComp.add(new Empl("John",6000));

       salComp.add(new Empl("Crish",2000));

       salComp.add(new Empl("Tom",2400));

       for(Empl e:salComp){

           System.out.println(e);

       }

   }

}

class MyNameComp implements Comparator<Empl>

{

 @Override

   public int compare(Empl e1, Empl e2) {

       return e1.getName().compareTo(e2.getName());

   }

}  

class MySalaryComp implements Comparator<Empl>{

   @Override

   public int compare(Empl e1, Empl e2) {

       if(e1.getSalary() > e2.getSalary()){

           return 1;

       } else {

           return -1;

       }

   }

}

class Empl{

    

   private String name;

   private int salary;

    

   public Empl(String n, int s){

       this.name = n;

       this.salary = s;

   }

    

   public String getName() {

       return name;

   }

   public void setName(String name) {

       this.name = name;

   }

   public int getSalary() {

       return salary;

   }

   public void setSalary(int salary) {

       this.salary = salary;

   }

   public String toString(){

       return "Name: "+this.name+"-- Salary: "+this.salary;

   }

}

Output

        Name: Crish– Salary: 2000

       Name: John– Salary: 6000

       Name: Ram– Salary: 3000

       Name: Tom– Salary: 2400

       ===========================

       Name: Crish– Salary: 2000

       Name: Tom– Salary: 2400

       Name: Ram– Salary: 3000

       Name: John– Salary: 6000

BACK

Filed in: Java Examples

Comments are closed.