Hashmap is key-value pair data structure. The key can be any data type. The most common used data types are string and integer. Sometime, custom defined object can be a key as well. Then you have to specify how to order them. This post introduces how to sort hashmap by key. In Java, you use TreeMap. In JavaScript and Python, you use lambda expressions.
In Java, you use TreeMap. You define the class that implements Comparable and write compareTo method to define the order. Then declare the Map as TreeMap. When adding data to a TreeMap, it automatically sort by key for you.
You use following lambda syntax to sort map by key, k represents key:
newMap = new Map([…mapObj].sort(([k1,v1], [k2,v2])=> { return k1.attrName – k2.attrName; }));
In Python, you use lambda expression lambda x, x[0] represents key:
newMap = sorted(mapObj.items(), key = lambda x: (x[0].attrName))
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
import java.util.Map; import java.util.TreeMap; class Fruit implements Comparable<Fruit>{ private String name; private double price; private String origin; public Fruit(String itm, double pr, String loc){ this.name = itm; this.price = pr; this.origin = loc; } public int compareTo(Fruit y){ return (int)(price-y.price); } public String toString(){ return name+ " " + price + " " + origin; } } public class HashMapObjectAsKey { public static void main(String a[]){ Map<Fruit, String> hm = new TreeMap<Fruit, String>(); hm.put(new Fruit("Banana", 20.13, "Hawaii"), "Mayflower"); hm.put(new Fruit("Apple", 40.09, "Japan"), "Queen Mary"); hm.put(new Fruit("Orange", 15.7, "Florida"), "Victory"); hm.put(new Fruit("Mango", 23.7, "California"), "Black Pearl"); //sort by key, Time O(nlog), Space O(n), n is number of fruits in map Map<Fruit, String> res = new TreeMap<Fruit, String>(hm); for (Fruit h: res.keySet()) { System.out.println(h + " -" + hm.get(h)); } } } |
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class Fruit { constructor(itm, pr, loc){ this.name = itm; this.price = pr; this.origin = loc; } toString() { return this.name + " " + this.price + " " + this.origin; } } const fruit1 = new Fruit("Banana", 20.13, "Hawaii"); const fruit2 = new Fruit("Apple", 40.09, "Japan"); const fruit3 = new Fruit("Orange", 15.7, "Florida"); const fruit4 =new Fruit("Mango", 23.7, "Philippines"); //object as key const fruitAsKey = new Map(); fruitAsKey.set(fruit1, "Mayflower"); fruitAsKey.set(fruit2, "Queen Mary"); fruitAsKey.set(fruit3, "Victory"); fruitAsKey.set(fruit4, "Black Pearl"); //sort by key, Time O(nlogn), Space O(n), n is number of fruits in map var res1 = new Map([...fruitAsKey].sort(([k1,v1], [k2,v2])=> { return k1.price - k2.price; })); console.log(res1); |
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
class Fruit : def __init__(self, itm, pr, loc): self.name = itm self.price = pr self.origin = loc def __str__(self): return self.name + " " + str(self.price) + " " + self.origin fruit1 = Fruit("Banana", 20.13, "Hawaii") fruit2 = Fruit("Apple", 40.09, "Japan") fruit3 = Fruit("Orange", 15.7, "Florida") fruit4 = Fruit("Mango", 23.7, "Philippines") #object as key fruitAsKey = {} fruitAsKey[fruit1]= "Mayflower" fruitAsKey[fruit2] = "Queen Mary" fruitAsKey[fruit3] = "Victory" fruitAsKey[fruit4] = "Black Pearl" #sort by key, Time O(nlogn), Space O(n), n is number of fruits in map res1 = sorted(fruitAsKey.items(), key = lambda x: (x[0].price)) for k, v in res1: print(str(k) + " - " + v) print() |
Output:
Mango 15.7 Philippines – Black Pearl
Banana 20.13 Hawaii – Mayflower
Orange 30.7 Florida – victory
Apple 40.09 Japan – Queen Mary
O Notation:
Time complexity: O(nlogn), n is the number of elements in hash.
Space complexity: O(n)