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.Collections;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class Main {
    public static List mapToEntryList(Map map) {
        return mapToEntryList(map, true);
    }

    /**
     * Build a List from the entry set of a map.
     * 
     *  <p>
     * NOTE: This should not be necessary but there are bugs
     * when you iterate using map.entrySet().iterator().
     * </p>
     * 
     * @param map the map to use as the source
     * @param sortedByValue whether or not to sort the values
     * of the created list
     * @return a list containing all values of the given
     * map
     */
    public static List mapToEntryList(Map map, boolean sortedByValue) {
        List retList = new ArrayList();
        if (map.isEmpty()) {
            return Collections.EMPTY_LIST;
        }
        Iterator it = map.keySet().iterator();
        while (it.hasNext()) {
            Object key = it.next();
            Object obj = map.get(key);
            retList.add(obj);
        }

        // Sort
        if (sortedByValue)
            Collections.sort(retList);
        return retList;
    }
}