<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tommy Chheng &#187; java</title>
	<atom:link href="http://tommy.chheng.com/index.php/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://tommy.chheng.com</link>
	<description>All Things Programming!</description>
	<lastBuildDate>Wed, 11 Aug 2010 05:58:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Sorting a HashMap by Value in Java vs Ruby</title>
		<link>http://tommy.chheng.com/index.php/2009/12/sorting-a-hashmap-by-value-in-java-ruby/</link>
		<comments>http://tommy.chheng.com/index.php/2009/12/sorting-a-hashmap-by-value-in-java-ruby/#comments</comments>
		<pubDate>Sat, 12 Dec 2009 16:40:15 +0000</pubDate>
		<dc:creator>tommy</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[hashmap]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://tommy.chheng.com/?p=176</guid>
		<description><![CDATA[I&#8217;m working a graph problem in Java where I need to sort nodes by its edge count. I have a HashMap of nodes to edge count so I just need to sort a HashMap by its value. A quick Google search brought up a few solutions. Below is a snippet of a typical solution from [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m working a graph problem in Java where I need to sort nodes by its edge count. I have a HashMap of nodes to edge count so I just need to sort a HashMap by its value.<br />
A quick Google search brought up a few solutions. Below is a snippet of a typical solution from <a href="http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java">StackOverFlow</a></p>
<pre>
<code>
public static <K, V extends Comparable<? super V>> List<K> getKeysSortedByValue(Map<K, V> map) {
    final int size = map.size();
    final List<Map.Entry<K, V>> list = new ArrayList<Map.Entry<K, V>>(size);
    list.addAll(map.entrySet());
    final ValueComparator<V> cmp = new ValueComparator<V>();
    Collections.sort(list, cmp);
    final List<K> keys = new ArrayList<K>(size);
    for (int i = 0; i < size; i++) {
        keys.set(i, list.get(i).getKey());
    }
    return keys;
}
private static final class ValueComparator<V extends Comparable<? super V>>
                                     implements Comparator<Map.Entry<?, V>> {
    public int compare(Map.Entry<?, V> o1, Map.Entry<?, V> o2) {
        return o1.getValue().compareTo(o2.getValue());
    }
}
</code>
</pre>
<p>Can you believe it?? >10 lines of verbose madness! The Ruby solution is:<br />
<code><br />
>> x = {:a => 1, :b => 2, :c => 1, :d => 5}<br />
>> x.keys.sort_by{|k| x[k]}<br />
=> [:a, :c, :b, :d]<br />
</code></p>
<p>This makes you really appreciate closure support in languages. Java 7&#8242;s support of closure should bring more sanity to the Java world.</p>
<p>I&#8217;m also learning Clojure right now, the solution is just as nice as Ruby.<br />
<code><br />
user=> (def x {:a 1 :b 2 :c 1 :d 5})<br />
user=> (sort-by #(x %) (keys x))<br />
(:a :c :b :d)<br />
</code></p>
<p>edit:<br />
In Scala, it is also very succinct:</p>
<p><code><br />
scala> val x = Map('a' -> 1, 'b' -> 2, 'c' -> 1, 'd' -> 5)<br />
x: scala.collection.immutable.Map[Char,Int] = Map((a,1), (b,2), (c,1), (d,5))<br />
scala> x.keysIterator.toList.sortWith((key1,key2) => x(key1) < x(key2))<br />
res5: List[Char] = List(a, c, b, d)<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://tommy.chheng.com/index.php/2009/12/sorting-a-hashmap-by-value-in-java-ruby/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
