Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Thursday, September 5, 2024

Anonymous Comparator In Java For Sorting

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.

Let us see the simple examples of List of Strings and Strig array sorting with an anonymous comparator approach.

Anonymous Comparator In Java


2. Anonymous Comparator Java To Sort List of Strings


In the below code, we are creating an ArrayList with String objects. Now we need to sort the list using the anonymous comparator approach using collections.sort().

Collectins.sort() method can be used to sort the custom objects also.

Example 1
package com.javaprogramto.java8.comparator.anonymous;

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

public class AnonymousComparatorListExample {

	public static void main(String[] args) {

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

		strings.add("abc");
		strings.add("mno");
		strings.add("def");
		strings.add("xyz");
		strings.add("ghi");

		System.out.println("List before sorting - " + strings);

		Collections.sort(strings, new Comparator() {

			@Override
			public int compare(Object o1, Object o2) {

				String str1 = (String) o1;
				String str2 = (String) o2;
				return str1.compareTo(str2);
			}

		});

		System.out.println("List after sorting with anonymous comparator - " + strings);

	}

}

Output
List before sorting - [abc, mno, def, xyz, ghi]
List after sorting with anonymous comparator - [abc, def, ghi, mno, xyz]


3. Anonymous Comparator Java To Sort String Array or Any type of Array


The comparator interface can be used to sort arrays in java. But now we will see how to sort an array using an anonymous comparator with Arrays.sort() method.


Example 3
package com.javaprogramto.java8.comparator.anonymous;

import java.util.Arrays;
import java.util.Comparator;

public class AnonymousComparatorArrayExample {

	public static void main(String[] args) {

		String[] stringArray = new String[5];

		stringArray[0] = "abc";
		stringArray[1] = "mno";
		stringArray[2] = "def";
		stringArray[3] = "xyz";
		stringArray[4] = "ghi";

		System.out.println("Array before sorting - " + stringArray);

		Arrays.sort(stringArray, new Comparator() {

			@Override
			public int compare(Object o1, Object o2) {

				String str1 = (String) o1;
				String str2 = (String) o2;
				return str1.compareTo(str2);
			}

		});

		System.out.println("Array after sorting with anonymous comparator - " + stringArray);

	}

}

Output

This program also uses a comparator using anonymous class and pass to sort() method as an argument.

Output is the same as the above section.

Array before sorting - [Ljava.lang.String;@d716361
Array after sorting with anonymous comparator - [Ljava.lang.String;@d716361


4. Conclusion


In this article, we've seen how to use an anonymous comparator with the Collections.sort() and Arrays.sort() method in java.





No comments:

Post a Comment

Please do not add any spam links in the comments section.