Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

public class Main {

    public static <K, V extends Comparable<? super V>> List<Map.Entry<K, V>> sortMapByValue(Map<K, V> map,
            final boolean reverse) {
        if (isEmpty(map)) {
            return null;
        }

        List<Map.Entry<K, V>> list = new ArrayList<Map.Entry<K, V>>(map.entrySet());

        Collections.sort(list, new Comparator<Map.Entry<K, V>>() {

            public int compare(Map.Entry<K, V> entry1, Map.Entry<K, V> entry2) {
                int result = entry1.getValue().compareTo(entry2.getValue());
                if (reverse) {
                    result *= -1;
                }

                return result;
            }
        });

        return list;
    }

    public static <T> boolean isEmpty(Collection<T> col) {
        if (col == null || col.isEmpty()) {
            return true;
        }

        return false;
    }

    public static <K, V> boolean isEmpty(Map<K, V> map) {
        if (map == null || map.isEmpty()) {
            return true;
        }

        return false;
    }
}