All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jfcutils.util.MapUtil Maven / Gradle / Ivy

Go to download

This is a Java API with utilities to work with files, strings and HTTP connections

The newest version!
package org.jfcutils.util;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * Utility for Maps
 * @author celli
 *
 */
public class MapUtil {
	
	/**
	 * Sort a Map by Value
	 * @param map the map to sort, the value type must be Comparable
	 * @param  object to compare
	 * @param  object to compare
	 * @return the sorted map
	 */
	public static > Map sortByValueDescending( Map map ) {
		List> list = new LinkedList>( map.entrySet() );
		Collections.sort( list, new Comparator>()
				{
			public int compare( Map.Entry o1, Map.Entry o2 )
			{
				return (o2.getValue()).compareTo( o1.getValue() );
			}
				} );

		Map result = new LinkedHashMap();
		for (Map.Entry entry : list)
		{
			result.put( entry.getKey(), entry.getValue() );
		}
		return result;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy