问题是,Perl里面只用一行的 Sort Hash by Value.
现在要写这么多?
class ComparatorByValue implements Comparator {
public int compare(Object o1, Object o2) {
Map.Entry s1 = (Map.Entry) o1;
Map.Entry s2 = (Map.Entry) o2;
return ((Integer)s2.getValue()).compareTo((Integer)s1.getValue());
}
public boolean equals(Object o) {
return compare(this, o)==0;
}
}
TreeSet keySet = new TreeSet ( new ComparatorByValue ());
keySet.addAll (tm.entrySet());
Iterator iterator = keySet.iterator();
while( iterator.hasNext() ) {
Map.Entry entry = (Map.Entry)iterator.next();
Integer myVal = (Integer) entry.getValue();
if ( myVal.intValue() > 10 ) {
System.out.println(entry.getKey() + "/" + entry.getValue()); }
}
现在要写这么多?
class ComparatorByValue implements Comparator {
public int compare(Object o1, Object o2) {
Map.Entry s1 = (Map.Entry) o1;
Map.Entry s2 = (Map.Entry) o2;
return ((Integer)s2.getValue()).compareTo((Integer)s1.getValue());
}
public boolean equals(Object o) {
return compare(this, o)==0;
}
}
TreeSet keySet = new TreeSet ( new ComparatorByValue ());
keySet.addAll (tm.entrySet());
Iterator iterator = keySet.iterator();
while( iterator.hasNext() ) {
Map.Entry entry = (Map.Entry)iterator.next();
Integer myVal = (Integer) entry.getValue();
if ( myVal.intValue() > 10 ) {
System.out.println(entry.getKey() + "/" + entry.getValue()); }
}