1. Overview
In this tutorial, We'll learn how to sort arrays or lists using an anonymous comparator in java.
A complete guide on how to use comparator in java?
Anonymous class means creating the class and providing the implementation at the same without a class name.
For example, we have an interface Job and it has the method post() to post the new job. here, a new job1 instance is created for the implementation class of Job interface without a class name.
Example 1
interface Job { void post(); } Job job1 = new Job() { @Override public void post() { System.out.println("Posting job 1 now"); }
This is similar to the anonymous implementation of the abstract class or interface. This is the actual implementation of the Comparator without creating a class and implementing the Comparator interface.