Heading

Wednesday, November 20, 2013

Sorting Employee based on Name of the Employee using collections in java

package CollectionsExamples;

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) {
        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);
li.add(new Employee1(7,"Ajay",24));
li.add(new Employee1(7,"bala",24));
System.out.println(li);
Collections.sort(li, new MyComparator1());
System.out.println(li);

    }
  
}

________________________

package CollectionsExamples;

import java.util.Comparator;


public class MyComparator1 implements Comparator<Employee1> {

    public int compare(Employee1 o1, Employee1 o2) {
       
        String name1=o1.getName();
        String name2=o2.getName();
        return name1.compareTo(name2);
    }

}

Sorting List with collection of Strings in Java

Sorting String List using Collections Sort Behaviour

package collections;

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

public class CollectionSortExample {

    public static void main(String[] args) {

    List<String> li=new ArrayList<String>();
    li.add("pavan");
    li.add("ravi");
    li.add("chaitu");
    li.add("basha");
    li.add("vasu");
    li.add("shami");
    System.out.println("Before Sorting List:    "+li);
    Collections.sort(li);
    System.out.println("\nAfter Sorting the List:"+li);
   
    }
}
O/P:
Before Sorting List:    [pavan, ravi, chaitu, basha, vasu, shami]

After Sorting the List:[basha, chaitu, pavan, ravi, shami, vasu]

Converting a number into single Digit

public class SingleNumber  {
public void m1()
{
System.out.println("hai");
}
    public static void main(String[] args) {
        int num=99998,sum=0;
        while(num>9)
        {
             sum=0;
            while(num!=0)
            {
            int r=num%10;
            sum=sum+r;
            num=num/10;
            }
               num=sum;
        }   
        System.out.println(sum);
        }
    }

Save and run this program u will get out put as shown below:

8

Sorting an Array in java

package collections;

import java.util.Arrays;

public class ArraySortExample {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] i={10,6,8,25,99,100};
        System.out.println("Before Sorting:");
        for(int j:i)
            System.out.print(j+" ");
        Arrays.sort(i);
        System.out.println("\n After Sorting:");
        for(int k=0;k<i.length;k++)
            System.out.print(i[k]+" ");
    }

}

O/P:
Before Sorting:
10 6 8 25 99 100
 After Sorting:
6 8 10 25 99 100

panchayat secretary recruitment 2013