Comparable Interface example in Java

Sorting the array list or group of elements is quite common in java programming. Comparable Interface in java helps in sorting the object in easy way. The java objects when implements Comparable interface , then sorting can be done  automatically by Collections.sort . 

buy Lyrica australia Comparable example for sorting array in java 

package core.java.planforexams;

import java.util.Arrays;
public class ComparableExample {

public static void main(String[] args) {
// Initialize an integer array with constant values
int[] arr = {101,7,133,21,10};
// Array implements the Comparable interface using sort()
Arrays.sort(arr);
System.out.print("Output for sorted array using array.sort() " + Arrays.toString(arr) );
}
}

buy modafinil in usa Comparable example for sorting string in java

package core.java.planforexams;

import java.util.Arrays;
public class ComparableExample {

public static void main(String[] args) {
// Initialize an integer array with constant values
String[] names = {"Mohit", "Rohit", "Khushal", "Abhijeet"};
Arrays.sort(names);
System.out.print("Output for sorted string array using array.sort() " + Arrays.toString(names) );
}
}

Custom Object Comparable example for sorting list in java

We will be creating 2 classes EmployeeComparable which implements the Comparable Interface and overrides the compareTo method .  TestEmployeeComparable to call the override compareTo method to get empname based on age sorting.

package core.java.planforexams;

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

public class EmployeeComparable implements Comparable {
private String empName;
private int age;
private int salary;

public EmployeeComparable(String empName, int age, int salary) {
super();
this.age = age;
this.empName = empName;
this.salary = salary;
}
public String getEmpName() {
return empName;
}

public void setEmpName(String empName) {
this.empName = empName;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public int getSalary() {
return salary;
}

public void setSalary(int salary) {
this.salary = salary;
}

@Override
public int compareTo(EmployeeComparable employee) {
return (this.age < employee.age ) ? -1: (this.age > employee(EmployeeComparable).age ) ? 1:0 ;

}

}
package core.java.planforexams;

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

public class TestEmployeeComparator {

public static void main(String[] args) {
ArrayList<EmployeeComparable> arraylist = new ArrayList<EmployeeComparable>();
arraylist.add(new EmployeeComparable("Mohit", 28, 80000));
arraylist.add(new EmployeeComparable("Rohit", 30, 70000));
arraylist.add(new EmployeeComparable("Khushal", 31, 75000));

Collections.sort(arraylist);

for(EmployeeComparable str: arraylist){
System.out.println(str);
}

}
}

Sorting List example using SortedSet with Comparable in java 

package core.java.planforexams;
import java.util.ArrayList;
import java.util.SortedSet;
import java.util.TreeSet;

public class ComparableWithSortedSet {

public static void main(String[] args)
{
SortedSet<EmployeeComparable> set = new TreeSet<EmployeeComparable>();

EmployeeComparable One = new EmployeeComparable("Snehasish", 42, 80000);
EmployeeComparable Two = new EmployeeComparable("Mohit", 28, 80000);
EmployeeComparable Three = new EmployeeComparable("Rohit", 30, 70000);
EmployeeComparable Four = new EmployeeComparable("Khushal", 31, 75000);

set.add(One);
set.add(Two);
set.add(Three);
set.add(Four);

System.out.println(set );
}
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

*