Skip to main content

How to sort Hashmap by its value or key - sort any map

Mostly asked coding question in product based company. concepts of maps and there implementation are being asked in many product based companies.

What you will learn in this tutorials ?

1. How to sort map ?

2. How to sort Hash map ?

3. How to Sort map using its key ?

4. How to sort map using its value ?

5. Sort map using its key in ascending or descending order / alphabatically or alphabatically reverse order.

These are the check your map concepts

How to sort hashmap by it's key or value ? 

what is hashmap?

Hashmap is a data Structure to store value in for key and values

Map<String, Integer> hmap = new HashMap<String,Integer>

Steps to sort hashmap

Solution in java:-

github link:- DataStructureAndAlgorithms/HashMapMain.java at master · pushpendragupta1993/DataStructureAndAlgorithms (github.com)


Java code to sort hashmap by its value

package com.training;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
public class HashMapMain {
public static void main(String args[]) {
HashMap<String, String> countryCapitalMap = new HashMap<String, String>();
countryCapitalMap.put("Australia", "Canberra");
countryCapitalMap.put("France", "Paris");
countryCapitalMap.put("Russia", "Moscow");
countryCapitalMap.put("India", "Delhi");
Set<Entry<String, String>> countryCapitalEntrySet = countryCapitalMap.entrySet();
List<Entry<String, String>> entryList = new ArrayList<Entry<String, String>>(countryCapitalEntrySet);
Collections.sort(entryList, new Comparator<Entry<String, String>>() {
@Override
public int compare(Entry<String, String> o1, Entry<String, String> o2) {


// sort using value ascending or alphabetically order                                 return o1.getValue().compareTo(o2.getValue());                                 // sort using value descending or alphabetically revers order                                 return o2.getValue().compareTo(o1.getValue());                                 // sort using key ascending or alphabetically order                                 return o1.getKey().compareTo(o2.getKey());                                 // sort using key descending or alphabetically revers order                                 return o1.getKey().compareTo(o2.getKey());
}
});
// Using LinkedHashMap to keep entries in sorted order
LinkedHashMap<String, String> sortedHashMap = new LinkedHashMap<String, String>();
for (Entry<String, String> entry : entryList) {
sortedHashMap.put(entry.getKey(), entry.getValue());
}
for (String countryKey : sortedHashMap.keySet()) {
System.out.println(countryKey + " -> " + sortedHashMap.get(countryKey));
}
}
} Video with explanation of the code

Comments

Popular posts from this blog

How to import project directly from git hub or bitbucket to eclipse or Spring tool suit (STS)

 In This tutorials we will see how we can directly import any repository or project from git hub or bitbucket to our IDE What we will learn in this tutorials ? 1. How to checkout repository from git hub or bitbucket to our IDE. 2. How to Import project from git hub repository to our eclipse or spring tool suit workspace. 3. How to create branch from java IDE like eclipse or Spring tool suit. 4. How to commit code from our IDE Thanks for watching

How to create new git branch from eclipse or Spring tool suit/STS

How to create new git branch from existing branch using IDE like eclipse, Spring tool suit, my eclipse. This process is also applicable other source code repositories like bitbucket  What you will learn in this tutorial ? 1. How to checkout code from git hub to any IDE ( eclipse, Spring tool suit, my eclipse ) 2. How to create branch using IDE 3. How to switch to a different branch using any IDE. 4. How to commit code changes and push the changes back to git hub or bit bucket Please check below details with hands on:- Thanks for watching this video hope you liked the video and cleared your understanding. How to create new git branch from eclipse How to create new git branch from Spring tool suit How to create new git branch from myeclipse How to create new git branch from any java IDE How to switch new git branch from eclipse How to switch new git branch from Spring tool suit How to switch new git branch from myeclipse How to switch new git branch from any java IDE How to import ...

How to debug any java application using eclipse, Spring tool suit, STS, Eclipse

In This tutorial we will see how to can debug any type of application in below mentioned IDEs Eclipse Spring tool suit STS Eclipse What you will learn in this tutorials ? 1. How to debug any java application. 2. How to debug restful webservice. 3. How to add break point. 4. How to step into and step in each step in java application while debug. 5. How to start using debugging. Please check the video mentioned below to have complete clarity and hands on. How to debug in eclipse, eclipse productivity, eclipse shortcuts, in28minutes tutorial, eclipse tutorial, pushpendra Gupta, eclipse java tutorial, java, eclipse tips, interview, eclipse tutorial for beginners, Karanam, Java , Eclipse , Debug , Java Code Debug , Source Code, How to add breakpoint in code, how to debug any java program, how to debug restful api, learn how to debug any code, how to debug any program in Spring tool suit, debug in STS

Find the length of string without using length method in java

In This tutorial we are going to see on of the tricky java coding question for fresher where they will be asked to write code to find length of any string without using any inbuild function or method. How to Find the length of string without using length method in java ? Lets see how we can do it  checkout the video mentioned below to understand each line of code, we will also debug the code and see what is happening in each line of code. Git repo:- https://github.com/pushpendragupta1993/DataStructureAndAlgorithms/blob/master/src/com/training/StringLength.java Find the length of string without using length method in java find length of string, learn java with me, find length of string how to find length of string in java. easiest way to learn coding easiest way to learn programming easiest way to understand Data structure easiest way to undestand algorithms easiest way to debug java easiest way to understand programs easiest way to understand java program easiest way to un...