Check this one also
Sort employee object in descending order
Here I am taking class Employee:

package com.instanceofjava;

import java.util.Comparable;
public class Employee implements Comparable<Employee> {
 int id;
 String name;
 public Employee(int id, String name) {;;
  public int getId() {
  return id;
 public void setId(int id) { = id;
 public String getName() {
  return name;
 public void setName(String name) { = name;
 public int compareTo(Employee e) {
 Integer i= this.getId();
 Integer j=e.getId();
     if (this.getId() == e.getId())
       return 0;
      if (this.getId() < e.getId())
        return 1;
      if (this.getId() > e.getId())
       return -1;
      return 0;


Here I am taking another class Main.Here I am adding objects to the My list

package com.oops;
import java.util.ArrayList;  
import java.util.Collections;
import java.util.Iterator;
class Main{  
public static void main(String args[]){  
ArrayList al=new ArrayList();  
al.add(new Employee(101,"Indhu"));  
al.add(new Employee(106,"Sindhu"));  
al.add(new Employee(105,"Swathi"));  
Iterator itr=al.iterator();  
Employee st=(Employee);  
System.out.println( +" " );  


Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
Newer Post
Older Post

No comments

Leave a Reply

Select Menu