Monday, January 12, 2015

TreeSet with user defined objects and Sub set and Element from a Set Pro in Java



How to sort a TreeSet with user defined objects

package com.java2novice.treeset;
import java.util.Comparator;
import java.util.TreeSet;
public class MyCompUserDefine
{
public static void main(String a[])
{
        TreeSet nameComp = new TreeSet(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 salComp = new TreeSet(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
{
 public int compare(Empl e1, Empl e2)
{
        return e1.getName().compareTo(e2.getName());
    }
}  
class MySalaryComp implements Comparator
{
          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;
    }
}

How to get subset from sorted set

package com.java2novice.treeset;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
public class MySetSublist
{
 public static void main(String a[])
{
        TreeSet ts = new TreeSet(new MyStrComp());
        ts.add("Red");
        ts.add("Orange");
        ts.add("Blue");
        ts.add("Green");
        ts.add("White");
        ts.add("Brown");
        ts.add("Yellow");
        ts.add("Black");
        System.out.println(ts);
        Set subSet = ts.subSet("Green", "White");
        System.out.println("sub set: "+subSet);
        subSet = ts.subSet("Green", true, "White", true);
        System.out.println("sub set: "+subSet);
        subSet = ts.subSet("Green", false, "White", true);
        System.out.println("sub set: "+subSet);
    }
}
class MyStrComp implements Comparator
{
public int compare(String str1, String str2)
{
        return str1.compareTo(str2);
    }
}

How to get least value element from a set

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

No comments:

Post a Comment