web
 

Comparable and Comparator in Java Example


Comparable and Comparator in Java are exceptionally valuable for arranging gathering of items. Java gives some inbuilt strategies to sort crude sorts exhibit or Wrapper classes cluster or rundown. Here we will initially figure out how we can sort an exhibit/rundown of crude kinds and wrapper classes and afterward we will utilize java.lang.Comparable and java.util.Comparator interfaces to sort cluster/rundown of custom classes.

Let's perceive how we can sort crude sorts or Object cluster and rundown with a basic program.

 

bundle com.journaldev.sort; 

import java.util.ArrayList; 

import java.util.Arrays; 

import java.util.Collections; 

import java.util.List; 

public class JavaObjectSorting { 

/** 

* This class tells the best way to sort crude clusters, 

* Wrapper classes Object Arrays 

* @param args 

*/ 

public static void main(String[] args) { 

//sort natives exhibit like int array 

int[] intArr = {5,9,1,10}; 

Arrays.sort(intArr); 

System.out.println(Arrays.toString(intArr)); 

//arranging String array 

String[] strArr = {"A", "C", "B", "Z", "E"}; 

Arrays.sort(strArr); 

System.out.println(Arrays.toString(strArr)); 

//arranging rundown of objects of Wrapper classes 

List<String> strList = new ArrayList<String>(); 

strList.add("A"); 

strList.add("C"); 

strList.add("B"); 

strList.add("Z"); 

strList.add("E"); 

Collections.sort(strList); 

for(String str: strList) System.out.print(" "+str); 

} 

} 

Output of the above program is:

 

[1, 5, 9, 10] 

[A, B, C, E, Z] 

A B C E Z 

Now how about we attempt to sort a variety of objects.

 

package com.journaldev.sort; 

public class Employee { 

private int id; 

private String name; 

private int age; 

private long compensation; 

public int getId() { 

return id; 

} 

public String getName() { 

return name; 

} 

public int getAge() { 

return age; 

} 

public long getSalary() { 

return compensation; 

} 

public Employee(int id, String name, int age, int salary) { 

this.id = id; 

this.name = name; 

this.age = age; 

this.salary = compensation; 

} 

@Override 

//this is abrogated to print the easy to use data about the Employee 

public String toString() { 

return "[id=" + this.id + ", name=" + this.name + ", age=" + this.age + ", salary=" + 

this.salary + "]"; 

} 

} 

Comparable and Comparator in Java are exceptionally valuable for arranging gathering of items. Java gives some inbuilt strategies to sort crude sorts exhibit or Wrapper classes cluster or rundown. Here we will initially figure out how we can sort an exhibit/rundown of crude kinds and wrapper classes and afterward we will utilize java.lang.Comparable and java.util.Comparator interfaces to sort cluster/rundown of custom classes.

Let's perceive how we can sort crude sorts or Object cluster and rundown with a basic program.

 

bundle com.journaldev.sort; 

import java.util.ArrayList; 

import java.util.Arrays; 

import java.util.Collections; 

import java.util.List; 

public class JavaObjectSorting { 

/** 

* This class tells the best way to sort crude clusters, 

* Wrapper classes Object Arrays 

* @param args 

*/ 

public static void main(String[] args) { 

//sort natives exhibit like int array 

int[] intArr = {5,9,1,10}; 

Arrays.sort(intArr); 

System.out.println(Arrays.toString(intArr)); 

//arranging String array 

String[] strArr = {"A", "C", "B", "Z", "E"}; 

Arrays.sort(strArr); 

System.out.println(Arrays.toString(strArr)); 

//arranging rundown of objects of Wrapper classes 

List<String> strList = new ArrayList<String>(); 

strList.add("A"); 

strList.add("C"); 

strList.add("B"); 

strList.add("Z"); 

strList.add("E"); 

Collections.sort(strList); 

for(String str: strList) System.out.print(" "+str); 

} 

} 

Output of the above program is:

 

[1, 5, 9, 10] 

[A, B, C, E, Z] 

A B C E Z 

Now how about we attempt to sort a variety of objects.

 

package com.journaldev.sort; 

public class Employee { 

private int id; 

private String name; 

private int age; 

private long compensation; 

public int getId() { 

return id; 

} 

public String getName() { 

return name; 

} 

public int getAge() { 

return age; 

} 

public long getSalary() { 

return compensation; 

} 

public Employee(int id, String name, int age, int salary) { 

this.id = id; 

this.name = name; 

this.age = age; 

this.salary = compensation; 

} 

@Override 

//this is abrogated to print the easy to use data about the Employee 

public String toString() { 

return "[id=" + this.id + ", name=" + this.name + ", age=" + this.age + ", salary=" + 

this.salary + "]"; 

} 

} 
public class Employee implements Comparable<Employee> { private int id; private String name; private int age; private long compensation; public int getId() { return id; } public String getName() { return name; } public int getAge() { return age; } public long getSalary() { return compensation; } public Employee(int id, String name, int age, int salary) { this.id = id; this.name = name; this.age = age; this.salary = compensation; } @Override public int compareTo(Employee emp) { //how about we sort the representative dependent on an id in rising order //restores a negative number, zero, or a positive whole number as this representative id //is not exactly, equivalent to, or more prominent than the predefined object. return (this.id - emp.id); } @Override //this is required to print the easy to understand data about the Employee public String toString() { return "[id=" + this.id + ", name=" + this.name + ", age=" + this.age + ", salary=" + this.salary + "]"; } /** * Comparator to sort representatives rundown or cluster arranged by Salary */ public static Comparator<Employee> SalaryComparator = new Comparator<Employee>() { @Override public int compare(Employee e1, Employee e2) { return (int) (e1.getSalary() - e2.getSalary()); } }; /** * Comparator to sort representatives rundown or exhibit arranged by Age */ public static Comparator<Employee> AgeComparator = new Comparator<Employee>() { @Override public int compare(Employee e1, Employee e2) { return e1.getAge() - e2.getAge(); } }; /** * Comparator to sort representatives rundown or exhibit arranged by Name */ public static Comparator<Employee> NameComparator = new Comparator<Employee>() { @Override public int compare(Employee e1, Employee e2) { return e1.getName().compareTo(e2.getName()); } }; }

Here is the different class execution of Comparator interface that will think about two Employees object first on their id and in the event that they are same, at that point on the name.

 

package com.journaldev.sort; 

import java.util.Comparator; 

public class EmployeeComparatorByIdAndName implements Comparator<Employee> { 

@Override 

public int compare(Employee o1, Employee o2) { 

int banner = o1.getId() - o2.getId(); 

if(flag==0) banner = o1.getName().compareTo(o2.getName()); 

return banner; 

} 

} 

Here is the test class where we are utilizing distinctive approaches to sort Objects in java utilizing Comparable and Comparator.

 

bundle com.journaldev.sort; 

import java.util.Arrays; 

public class JavaObjectSorting { 

/** 

* This class tells the best way to sort custom articles cluster/list 

* executing Comparable and Comparator interfaces 

* @param args 

*/ 

public static void main(String[] args) { 

//arranging custom article array 

Employee[] empArr = new Employee[4]; 

empArr[0] = new Employee(10, "Mikey", 25, 10000); 

empArr[1] = new Employee(20, "Arun", 29, 20000); 

empArr[2] = new Employee(5, "Lisa", 35, 5000); 

empArr[3] = new Employee(1, "Pankaj", 32, 50000); 

//arranging representatives cluster utilizing Comparable interface implementation 

Arrays.sort(empArr); 

System.out.println("Default Sorting of Employees list:\n"+Arrays.toString(empArr)); 

//sort representatives cluster utilizing Comparator by Salary 

Arrays.sort(empArr, Employee.SalaryComparator); 

System.out.println("Employees list arranged by Salary:\n"+Arrays.toString(empArr)); 

//sort representatives exhibit utilizing Comparator by Age







journaldev is optimized for learning.© journaldev .
All Right Reserved and you agree to have read and accepted our term and condition