Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import java.util.Map;
import java.util.Map.Entry;

public class Main {
    public static List<Map.Entry<String, Double>> sortMapByValue(Map<String, Double> map) {
        List<Map.Entry<String, Double>> list = new ArrayList<Map.Entry<String, Double>>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<String, Double>>() {
            @Override
            public int compare(Entry<String, Double> o1, Entry<String, Double> o2) {
                double result = o2.getValue() - o1.getValue();
                if (result > 0)
                    return -1;
                else if (result == 0)
                    return 0;
                else
                    return 1;
            }
        });
        return list;
    }
}