Monday, January 12, 2015

Java Using Element from a set and user defined objects in TreeSet in Java



How to get highest value element from a set

package com.java2novice.treeset;
import java.util.Comparator;
import java.util.TreeSet;
public class MyHighSalEmp
{
public static void main(String a[])
{   
   TreeSet salComp = new TreeSet(new MySalCompr1());
        salComp.add(new Empl2("Ram",3000));
        salComp.add(new Empl2("John",6000));
        salComp.add(new Empl2("Crish",2000));
        salComp.add(new Empl2("Tom",2400));
        System.out.println("Highest salary emp: "+salComp.last());
    }
}
class MySalCompr1 implements Comparator
{
public int compare(Empl2 e1, Empl2 e2)
{
        if(e1.getSalary() > e2.getSalary())
        {
            return 1;
        }
        else
        {
            return -1;
        }
    }
}
class Empl2
{
    private String name;
    private int salary;
public Empl2(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;
    }
}

How to avoid duplicate user defined objects in TreeSet

package com.java2novice.treeset;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
public class MyUserDuplicates
{
public static void main(String a[])
{
        Set ts = new TreeSet(new EmpComp());
        ts.add(new Emp(201,"John",40000));
        ts.add(new Emp(302,"Krish",44500));
        ts.add(new Emp(146,"Tom",20000));
        ts.add(new Emp(543,"Abdul",10000));
        ts.add(new Emp(12,"Dinesh",50000));
        ts.add(new Emp(146,"Tom",20000));
        for(Emp e:ts)
       {
            System.out.println(e);
        }
    }
}
class EmpComp implements Comparator
 {
  public int compare(Emp e1, Emp e2)
 {
        if(e1.getEmpId() == e2.getEmpId())
          {
            return 0;
        }
         if(e1.getEmpId() < e2.getEmpId())
         {
            return 1;
        }
         else
        {
            return -1;
        }
    }
}
class Emp
{
    private int empId;
    private String empName;
    private int empSal;
 public Emp(int id, String name, int sal)
 {
        this.empId = id;
        this.empName = name;
        this.empSal = sal;
    }
 public int getEmpId()
 {
        return empId;
   }
  public void setEmpId(int empId)
  {
        this.empId = empId;
    }
   public String getEmpName()
  {
        return empName;
    }
   public void setEmpName(String empName)
  {
        this.empName = empName;
    }
     
    public int getEmpSal()
   {
        return empSal;
    }
    public void setEmpSal(int empSal)
   {
        this.empSal = empSal;
    }
    public String toString()
   {
        return empId+" : "+empName+" : "+empSal;
    }
}

No comments:

Post a Comment