Sort map by Key using Comparator

Category : Java | Sub Category : Java 8 Features | By Runner Dev Last updated: 2020-12-10 15:28:29 Viewed : 249


Sort map by Key using Comparator  

1. java.util.Comparator<Employee> 

@FunctionalInterface

A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order. Comparators can also be used to control the order of certain data structures (such as sorted sets or sorted maps), or to provide an ordering for collections of objects that dont have a natural ordering.

The ordering imposed by a comparator c on a set of elements S is said to be consistent with equals if and only if c.compare(e1, e2)==0 has the same boolean value as e1.equals(e2) for every e1 and e2 in S.

2. Example:

HashMapSorting.java

 

 package javaprograms;

 

import java.util.Comparator;

import java.util.HashMap;

import java.util.LinkedHashMap;

import java.util.Map;

import java.util.stream.Collectors;

 

public class HashMapSorting {

   public static void main(String args[]){

 

        // Sort map by Key using Comparator

 

        Map<Employee, Integer> map = new HashMap<>();

        Employee emp0 = new Employee("RamPrasad""B");

        Employee emp1 = new Employee("Swetha""V");

        Employee emp3 = new Employee("Yashu""B");

        Employee emp2 = new Employee("Joshi""B");

 

        map.put(emp0, 4);

        map.put(emp1, 2);

        map.put(emp2, 1);

        map.put(emp3, 3);

 

        // Sort(FirstName) map by Key using Comparator

 Comparator<Employee> byFirstName = (Employee o1, Employee o2) -> o1.getFirstName().compareTo(o2.getFirstName());


LinkedHashMap<Employee, Integer> sortedMap = 

                                                map.entrySet().stream()

.sorted(Map.Entry.<Employee, Integer>comparingByKey(byFirstName))

                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1e2) -> e1, LinkedHashMap::new));

 

        System.out.println("sortedMap==>" + sortedMap);

   }

}

 

class Employee {

   private String firstName;

   private String lastName;

 

   @Override

   public String toString() {

        return "Employee [firstName=" + firstName + ", lastName=" + lastName + "]";

   }

 

   Employee(String firstName, String lastName) {

        this.firstName = firstName;

        this.lastName = lastName;

   }

 

   /**

    * @return the firstName

    */

   public String getFirstName() {

        return firstName;

   }

 

   /**

    * @param firstName the firstName to set

    */

   public void setFirstName(String firstName) {

        this.firstName = firstName;

   }

 

   /**

    * @return the lastName

    */

   public String getLastName() {

        return lastName;

   }

 

   /**

    * @param lastName the lastName to set

    */

   public void setLastName(String lastName) {

        this.lastName = lastName;

   }

 

}


ouput

 sortedMap==>

{Employee [firstName=Joshi, lastName=B]=1, Employee [firstName=RamPrasad, lastName=B]=4, Employee [firstName=Swetha, lastName=V]=2, Employee [firstName=Yashu, lastName=B]=3}

Search
Related Articles

Leave a Comment: