Heading

Saturday, October 26, 2013

Sorting Element of list using Comparable interface

  Here is an Example to sort list of added objects to an array list using Comparable  interface.
  import java.util.ArrayList;
    import java.util.Collections;

    public class Sort1 implements Comparable<Object>
    {
    int age;
    String name;
    Sort1(int age,String name)
    {
        this.age=age;
        this.name=name;
    }
    public String toString()
    {
        return "age:" +age+" "+ "name:" +name;
    }
    public int compareTo(Object o)
    {
        return ((Sort1)o).age-age;
    }
    public static void main(String[] args)
    {
        ArrayList<Sort1> list=new ArrayList<Sort1>();
        list.add(new Sort1(60,"byz"));
        list.add(new Sort1(10,"xbc"));
        list.add(new Sort1(20,"ahaitu"));
        Collections.sort(list);
        System.out.println(list);
      
      
    }
    }

Comparing Objects Added to list

Hi Friends



Here we are comparing added objects to list using  Comparator Interface.For this First declare the Employee class as shown below:



import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Employee1 {
    private int id;
    private String name;
    private int age;
 
    public Employee1(int id,String name,int age) {
        //super();
        this.age = age;
        this.id = id;
        this.name = name;
    }
    public int getId() {
        return id;
    }
    /*public void setId(int id) {
        this.id = id;
    }*/
    public String getName() {
        return name;
    }
    /*public void setName(String name) {
        this.name = name;
    }*/
    public int getAge() {
        return age;
    }
    /*public void setAge(int age) {
        this.age = age;
    }*/
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return id+" "+name+" "+age;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
List<Employee1> li=new ArrayList<Employee1>();
Employee1 e=new Employee1(9,"chaitu",26);
Employee1 e1=new Employee1(2,"mahesh",22);
Employee1 e2=new Employee1(8,"vinay",28);
Employee1 e3=new Employee1(6,"ravi",16);
li.add(e);
li.add(e1);
li.add(e2);
li.add(e3);
Collections.sort(li, new MyComparator1());
System.out.println(li);

    }
 
}

Then write the MyComparator1 Class  as shown below

import java.util.Comparator;


public class MyComparator1 implements Comparator<Employee1> {

    @Override
    public int compare(Employee1 o1, Employee1 o2) {
        // TODO Auto-generated method stub
        if(o1.getAge()>o2.getAge())
        return -1;
        else if(o1.getAge()<o2.getAge())
         
            return 1;
        else
        return 0;
        /*if(o1.getId()>o2.getId())
            return 1;
            else if(o1.getId()<o2.getId())
                return -1;
            else
            return 0;*/
    }

}

Now see this results your requirement.

Here it return results based on age in ascending order.

Wednesday, October 23, 2013

Singleton Example in java


public class Singleton {

    /**
     * @param args
     */
    private  static Singleton instance;
    private Singleton()
    {
       
    }
    public static Singleton createInstance()
    {
        if(instance==null)
        {
            instance=new Singleton();
        }
        return instance;
    }
    public static void main(String[] args) {
       
// TODO Auto-generated method stub
Singleton s1=Singleton.createInstance();
Singleton s2=Singleton.createInstance();
Singleton s3=Singleton.createInstance();
System.out.println(s1+ " "+s2+" "+s3);
    }

}