Java Comparable

Java Comparable interface is used to order the objects of the user-defined class. This interface is found in java.lang package and contains only one method named compareTo(Object)

Property of compareTo(Object obj) method

public int compareTo(Object obj): It is used to compare the current object with the specified object. It returns

  • a positive integer if the current object is greater than the specified object.
  • negative integer, if the current object is less than the specified object.
  • zero, if the current object is equal to the specified object.

Example 1:

[[Employee [name=Aamn, age=14], Employee [name=Ram Singh, age=23], Employee [name=Ram, age=25], Employee [name=Jeetu, age=26], Employee [name=Aman, age=36]]]

Approach: Natural ascending order sorting based on age.

Java


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

public class JavaComparable {
    public static void main(String[] args) {
        List<EmployeeDetailslist = new ArrayList<EmployeeDetails>();
        list.add(new EmployeeDetails("Ram Singh"23));
        list.add(new EmployeeDetails("Ram"25));
        list.add(new EmployeeDetails("Jeetu"26));
        list.add(new EmployeeDetails("Aman"36));
        list.add(new EmployeeDetails("Aamn"14));
        // using comparable
        Collections.sort(list);
        System.out.println(Arrays.asList(list));
    }
}

class EmployeeDetails implements Comparable<EmployeeDetails> {

    String name;
    int age;

    public EmployeeDetails() {
        super();
    }

    public EmployeeDetails(String nameint age) {
        super();
        this.name = name;
        this.age = age;
    }

    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() {
        return "Employee [name=" + name + ", age=" + age + "]";
    }

    @Override
    public int compareTo(EmployeeDetails o) {
        // ascending order
// for decending order return o.age-this.age;
        return this.age - o.age;

    }
}

Approach: Natural ascending order sorting based on age using multiple conditions.

Java


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

public class JavaComparable {
    public static void main(String[] args) {
        List<EmployeeDetailslist = new ArrayList<EmployeeDetails>();
        list.add(new EmployeeDetails("Ram Singh"23));
        list.add(new EmployeeDetails("Ram"25));
        list.add(new EmployeeDetails("Jeetu"26));
        list.add(new EmployeeDetails("Aman"36));
        list.add(new EmployeeDetails("Aamn"14));
        // using comparable
        Collections.sort(list);
        System.out.println(Arrays.asList(list));
    }
}

class EmployeeDetails implements Comparable<EmployeeDetails> {

    String name;
    int age;

    public EmployeeDetails() {
        super();
    }

    public EmployeeDetails(String nameint age) {
        super();
        this.name = name;
        this.age = age;
    }

    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() {
        return "Employee [name=" + name + ", age=" + age + "]";
    }

    @Override
    public int compareTo(EmployeeDetails o) {
        if (this.age == o.age)
            return 0;
        else if (this.age > o.age)
            return 1;
        else
            return -1;
    }

}


Approach: Natural ascending order sorting based on Name.

Java


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

public class JavaComparable {
    public static void main(String[] args) {
        List<EmployeeDetailslist = new ArrayList<EmployeeDetails>();
        list.add(new EmployeeDetails("Ram Singh"23));
        list.add(new EmployeeDetails("Ram"25));
        list.add(new EmployeeDetails("Jeetu"26));
        list.add(new EmployeeDetails("Aman"36));
        list.add(new EmployeeDetails("Aamn"14));
        // using comparable
        Collections.sort(list);
        System.out.println(Arrays.asList(list));
    }
}

class EmployeeDetails implements Comparable<EmployeeDetails> {

    String name;
    int age;

    public EmployeeDetails() {
        super();
    }

    public EmployeeDetails(String nameint age) {
        super();
        this.name = name;
        this.age = age;
    }

    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() {
        return "Employee [name=" + name + ", age=" + age + "]";
    }

    @Override
    public int compareTo(EmployeeDetails o) {
        return this.name.compareTo(o.name);
    }

}



No comments:

Post a Comment