How to Implement a Custom Comparator for a LinkedHashSet in Java? Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report In Java, we cannot directly implement a custom comparator for a LinkedHashSet. But sometimes we need a requirement of custom comparator for example custom sorting. So, we can use some alternative approaches to fulfill the requirements. In this article, we will be using a class for creating the custom comparator and this class will implement the Comparable interface for a LinkedHashSet in Java. Approach:In the first step, we have created a class which is the type of Employee, and implemented a Comparable interface.It accepts a value of Employee type in a constructor and accepts UserIDs.Then Override compareTo method. We also Override a toString method for printing the output.Now we create the LinkedHashSet of type Employee and add Employee ID.Then we add these LinkedHashSet values to an ArrayList.So, we can use Collection.Sort which sorts the elements by User IDs.Then we print the output. Program to Implement a Custom Comparator for a LinkedHashSet in JavaSuppose we have a LinkedHashSet of Employee IDs and want to perform a custom sorting, for this follow the below implementation. Java import java.util.ArrayList; import java.util.Collections; import java.util.LinkedHashSet; import java.util.List; // We need to implement the Comparable interface for creating // a custom comparator. class Employee implements Comparable<Employee> { private int employeeId; public Employee(int id) { this.employeeId = id; } public int getId() { return this.employeeId; } // We override the toString method. public String toString() { return "Employee -> " + getId(); } // Custom comparator public int compareTo(Employee otherEmployee) { return this.getId() - otherEmployee.getId(); } } public class GFG { public static void main(String[] args) { LinkedHashSet<Employee> setOfEmployees = new LinkedHashSet<>(); setOfEmployees.add(new Employee(3)); setOfEmployees.add(new Employee(1)); setOfEmployees.add(new Employee(2)); // Convert the set to a list for sorting List<Employee> listUsers = new ArrayList<>(setOfEmployees); // Sort the list using the custom comparator Collections.sort(listUsers); // Print the sorted list System.out.println(listUsers); } } Output[Employee -> 1, Employee -> 2, Employee -> 3] Explanation of the Program:In the above program, we have defined a class Employee that implements the Comparable interface. The compareTo method is implemented to compare Employee objects based on their employee IDs.We create a LinkedHashSet named setOfEmployees to store Employee objects. Note that LinkedHashSet maintains insertion order.We convert the LinkedHashSet to an ArrayList named listUsers to perform sorting. This is done to demonstrate sorting using the custom comparator.We sort the listUsers using Collections.sort method, which uses the custom comparator defined in the Employee class.Finally, we print the sorted list. Comment More infoAdvertise with us D devanshuma3v23 Follow Improve Article Tags : Java Java Programs Java-LinkedHashMap Java Examples Practice Tags : Java Explore BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOPs & InterfacesClasses and Objects in Java10 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial15+ min readSynchronization in Java10 min readFile Handling in Java5 min readJava Method References9 min readJava 8 Stream Tutorial15+ min readJava Networking15+ min readJDBC Tutorial12 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions7 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like