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<EmployeeDetails> list = 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 comparableCollections.sort(list);System.out.println(Arrays.asList(list));}}class EmployeeDetails implements Comparable<EmployeeDetails> {String name;int age;public EmployeeDetails() {super();}public EmployeeDetails(String name, int 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;}@Overridepublic String toString() {return "Employee [name=" + name + ", age=" + age + "]";}@Overridepublic 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<EmployeeDetails> list = 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 comparableCollections.sort(list);System.out.println(Arrays.asList(list));}}class EmployeeDetails implements Comparable<EmployeeDetails> {String name;int age;public EmployeeDetails() {super();}public EmployeeDetails(String name, int 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;}@Overridepublic String toString() {return "Employee [name=" + name + ", age=" + age + "]";}@Overridepublic int compareTo(EmployeeDetails o) {if (this.age == o.age)return 0;else if (this.age > o.age)return 1;elsereturn -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<EmployeeDetails> list = 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 comparableCollections.sort(list);System.out.println(Arrays.asList(list));}}class EmployeeDetails implements Comparable<EmployeeDetails> {String name;int age;public EmployeeDetails() {super();}public EmployeeDetails(String name, int 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;}@Overridepublic String toString() {return "Employee [name=" + name + ", age=" + age + "]";}@Overridepublic int compareTo(EmployeeDetails o) {return this.name.compareTo(o.name);}}
No comments:
Post a Comment