Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class Main {

    public static void main(String[] args) {

        HashMap<String, Double> map = new HashMap<String, Double>();
        ValueComparator bvc = new ValueComparator(map);
        TreeMap<String, Double> sorted_map = new TreeMap<String, Double>(bvc);

        map.put("A", 9.0);
        map.put("B", 6.0);
        map.put("C", 7.0);
        map.put("D", 10.0);

        System.out.println("unsorted map: " + map);

        sorted_map.putAll(map);

        System.out.println("results: " + sorted_map);
    }
}

class ValueComparator implements Comparator<String> {

    Map<String, Double> base;

    public ValueComparator(Map<String, Double> base) {
        this.base = base;
    }

    public int compare(String a, String b) {
        if (base.get(a) >= base.get(b)) {
            return -1;
        } else {
            return 1;
        }
    }
}