1. Home
  2. /
  3. Java
  4. /
  5. Custom Comparator Example

Custom Comparator Example - Java Typing CST Test

Loading…

Custom Comparator Example — Java Code

Sorts a list of custom objects using a custom comparator.

import java.util.*;

class Person {
	String name;
	int age;
	Person(String name, int age) { this.name = name; this.age = age; }
	public String toString() { return name + "(" + age + ")"; }
}

public class ComparatorExample {
	public static void main(String[] args) {
		List<Person> people = Arrays.asList(
		new Person("Alice", 25),
		new Person("Bob", 30),
		new Person("Charlie", 20)
		);
		people.sort(Comparator.comparingInt(p -> p.age));
		System.out.println(people);
	}
}

Java Language Guide

Java is a robust, object-oriented, platform-independent programming language designed for reliability, performance, and scalability. It powers enterprise systems, Android apps, backend services, banking infrastructure, and large distributed systems used globally.

Primary Use Cases

  • ▸Enterprise backend systems
  • ▸Android application development
  • ▸Financial/banking systems
  • ▸Cloud microservices (Spring Boot, Quarkus)
  • ▸Large distributed systems
  • ▸Big data pipelines (Hadoop, Spark)

Notable Features

  • ▸Platform independence via JVM
  • ▸Garbage collection for memory safety
  • ▸Rich standard library
  • ▸Modern functional features (lambdas, streams)
  • ▸Strong concurrency APIs
  • ▸Huge enterprise ecosystem (Spring)

Origin & Creator

Created by James Gosling at Sun Microsystems (1991-1995). Officially released in 1995. Evolved through major versions: Java 5 (Generics), Java 8 (Lambdas & Streams), Java 9+ (Modules), Java 17+ (LTS), Project Loom (virtual threads), and modern performance optimizations.

Industrial Note

Java dominates banking, fintech, insurance, enterprise ERPs, government software, and high-scale backend services (Netflix, Amazon, LinkedIn). It remains the strongest enterprise language due to JVM performance, stability, concurrency, and massive tooling ecosystem.

More Java Typing Exercises

Java Collection and Stream APIJava Basic Class and ObjectJava Map and HashMap ExampleJava List Sorting ExampleJava Exception Handling ExampleJava ForEach with LambdaJava Optional ExampleJava Filter and Collect ExampleJava Set Example

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPC#RubyMqlCqlN1qlCypherGremlin