Sort object using comparator

  1. package com.instanceofjava;
  2.  
  3. public class Employee 
  4. {
  5.  
  6.  int id;
  7.  String name;
  8.  
  9.  public Employee(int id, String name) {
  10.  
  11.   this.id=id;
  12.   this.name=name;
  13.  
  14.  }
  15.  
  16. public int getId() {
  17.  
  18. return id;
  19.  
  20. }
  21.  
  22.  public void setId(int id) {
  23.   this.id = id;
  24.  }
  25.  
  26.  public String getName() {
  27.   return name;
  28.  }
  29.  
  30. public void setName(String name) {
  31.  
  32.   this.name = name;
  33.  
  34. }
  35.  
  36. }


  1. class MyEmpComp implements Comparator<Employee >
  2.  
  3.  @Override
  4.  public int compare(Employee e1, Employee e2) {
  5.  
  6.         if(e1.getName() < e2.getName()){
  7.             return 1;
  8.         } else {
  9.             return -1;
  10.         }
  11.  
  12. }
  13.  
  14. }




  1. package com.oops;
  2.  
  3. import java.util.ArrayList;  
  4. import java.util.Collections;
  5. import java.util.Iterator;
  6.  
  7. import java.io.*;  
  8.  
  9. class Main{  
  10.  
  11. public static void main(String args[]){  
  12.  
  13. ArrayList al=new ArrayList();  
  14.  
  15. al.add(new Employee(101,"Indhu"));  
  16. al.add(new Employee(106,"Sindhu"));  
  17. al.add(new Employee(105,"Swathi"));
  18.   
  19. Collections.sort(al,MyEmpComp );  
  20.  
  21. Iterator itr=al.iterator();  
  22.  
  23. while(itr.hasNext()){   
  24.  
  25. Employee st=(Employee)itr.next();  
  26. System.out.println(st.id +" "+st.name );  
  27.  
  28.   }
  29.  
  30.   
  31. }
  32.  
  33.  


Output:
  1. Indhu
  2. Swathi
  3. Sindhu



Sort object using comparable interface

  1. package com.instanceofjava;
  2.  
  3. import java.util.Comparable;
  4.  
  5. public class Employee implements Comparable<Employee> {
  6.  
  7.  int id;
  8.  String name;
  9.  
  10.  public Employee(int id, String name) {
  11. this.id=id; 
  12. this.name=name;
  13.  }
  14.  
  15.  public int getId() {
  16. return id;
  17.  }
  18.  
  19.  public void setId(int id) {
  20.   this.id = id;
  21.  }
  22.  
  23.  public String getName() {
  24. return name;
  25.  }
  26.  
  27.  public void setName(String name) {
  28.   this.name = name;
  29.  }
  30.  
  31.  @Override
  32.  public int compareTo(Employee e) {
  33.  
  34.  Integer i= this.getId();
  35.  Integer j=e.getId();
  36.  
  37.      if (this.getId() == e.getId())
  38.        return 0;
  39.       if (this.getId() < e.getId())
  40.         return 1;
  41.       if (this.getId() > e.getId())
  42.        return -1;
  43.       return 0;
  44. }
  45. }

  1. package com.oops;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.Iterator;
  5. import java.io.*;
  6.   class Main{  
  7.  
  8. public static void main(String args[]){
  9.  
  10.  ArrayList al=new ArrayList();  
  11.  
  12.  al.add(new Employee(101,"Indhu"));
  13. al.add(new Employee(106,"Sindhu"));
  14.  al.add(new Employee(105,"Swathi"));  
  15. Collections.sort(al);
  16.  
  17. Iterator itr=al.iterator();   
  18.  
  19. while(itr.hasNext()){
  20.  
  21.    Employee st=(Employee)itr.next();
  22.    System.out.println(st.id +" "+st.name );   
  23.  
  24.   } 
  25. }  
  26.  
  27. }  
Output:
  1. Indhu 
  2. Swathi 
  3. Sindhu

 

Method overriding example program


  1. package com.oops; 
  2.  
  3. class A
  4.  
  5. void msg(){
  6.  
  7.  System.out.println("Hello");
  8.  
  9.  
  10. }

  1. class B extends A
  2. {
  3.  
  4.  void msg(){
  5.  
  6. System.out.println("Welcome");
  7.  
  8. }
  9.  
  10. public static void main(String args[])
  11.  
  12.     A a=new A();
  13.     B b=new B();
  14.      A obj=new B(); 
  15.  
  16.    System.out.println(a.msg());   //   Hello
  17.         System.out.println(obj.msg());  // Welcome
  18.         System.out.println(b.msg());   //Welcome
  19.  
  20. }  





Output:
  1. Hello
  2. Welcome 
  3. Welcome



Object cloning in java example

  1. package com.instanceofjava;
  2.  
  3. public class Employee implements Cloneable {
  4.  
  5.         int a=0; 
  6.         String name="";
  7.         Employee (int a,String name){ 
  8.         this.a=a;
  9.         this.name=name;
  10. }
  11.  
  12. public Employee clone() throws CloneNotSupportedException{
  13.  
  14. return (Employee ) super.clone();
  15.  
  16. }
  17.  
  18. public static void main(String[] args) {
  19.  
  20.           Employee e=new Employee (2,"Indhu");
  21.             System.out.println(e.name);
  22.  
  23. try {
  24.  
  25.  Employee b=e.clone();
  26.  System.out.println(b.name);
  27.  
  28. }
  29.  catch (CloneNotSupportedException e1) {

  30. e1.printStackTrace();
  31. }
  32. }
  33.  
  34. }




Output:
  1. Indhu
  2. Indhu

Sort integer array using Bubble Sort in java

  1. package com.instaceofjava;
  2.  
  3. public class Bubblesort {
  4.  
  5. public static void main(String[] args) {  
  6.  
  7. int a[]={23,5,6,66,1};
  8.  
  9. int n=a.length;
  10.  
  11.         int temp=0;
  12.  
  13. for(int i=0;i<n;i++){
  14.  
  15. for(int j=1;j<(n-i);j++){
  16.  
  17. if(a[j-1]>a[j]){
  18.    temp=a[j];
  19.   a[j]=a[j-1];
  20.   a[j-1]=temp;
  21.  
  22.   }  
  23.  
  24. }
  25.  
  26. }
  27.  
  28. for(int k=0;k<n;k++){
  29. System.out.println(a[k]);


  30. }



Output:
  1. 1,
  2. 5
  3. 6
  4. 23
  5. 66


How to find largest element in an array with index and value using array?


  • Lets see an example program to get largest element in an integer array. and also find second largest element in the same array.


  1. package com.instanceofjava; 
  2. public class Array {
  3.  
  4. public static void main(String[] args) {
  5.  
  6. int arr[]={1,120,56,78,87};
  7. int largest=arr[0];
  8. int smallest=arr[0];
  9. int small=0;
  10. int index=0;
  11.  
  12. for(int i=1;i<arr.length;i++){
  13.  
  14. if(arr[i]>largest){
  15.  
  16. largest=arr[i];
  17. index=i;
  18.  
  19. }
  20. else if(smallest>arr[i]){
  21.  
  22. smallest=arr[i];
  23. small=i;
  24.  
  25. }
  26. }
  27.  
  28. System.out.println(largest);
  29. System.out.println(index);
  30. System.out.println(smallest);
  31. System.out.println(small);
  32.  

  33. }


Output:
  1. 120
  2. 1
  3. 87
  4. 4


Select Menu