Category Archives: Source Code
Combinations of adding parentheses – code
Combinations of adding operators and parentheses
Permutation of multiple arrays and iterator – code

Permutation of multiple arrays and iterator has two tasks. First is the permutation of multiple arrays and output as an array of new combinations. The second is to implement the iterator of this new array. Permutation is an important topic in the fields of combinatorics. It is usually implemented using …
Royal succession order – code

Royal succession order is also known as the line of succession to the British throne. Under common law, the crown is inherited by a sovereign’s children or by a childless sovereign’s nearest collateral line. You can get the current British monarchy succession order here. As programmer, you might be asked …
Build nested object from hierarchical list- code
Find least common set – Outlier set
Sort with lambda comparator

Comparator is used to sort the objects by different attributes. You can simply put lambda in the place where requires Comparator. Lambda is preferred when the comparison is simple. Here are the syntax and examples in Java, JavaScript and Python. Sometime you may have complex comparisons involving multiple attributes. For example, …
Sort with Java Comparator
HashMap sort by key

In HashMap, the key can be any data type. The most common used data types are String and Integer. When you use custom defined object as key, you have to specify how to order them. In Java, you use TreeMap. In JavaScript and Python, you use lambda expressions.
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("Orange", 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)); } } } |
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); |
…