I created small utility which helps me sort my map by values (based on one of the StackOverflow answers to give credit):
public static <K, V extends Comparable> Map<K, V> sortMapByValues(Map<K, V> original) {
final ValueComparator<K, V> comparator = new ValueComparator<K, V>(original);
final Map<K, V> sorted = new TreeMap<K, V>(comparator);
sorted.putAll(original);
return sorted;
}
And the ValueComparator:
static class ValueComparator<K, V extends Comparable<V>> implements Comparator<K> {
private final Map<K, V> base;
public ValueComparator(Map<K, V> base) {
this.base = base;
}
public int compare(K a, K b) {
return base.get(b).compareTo(base.get(a));
}
}
Now this works pretty well when in my IntelliJ 10 IDE, the code runs and do what I expect.
But if I try to build the project by maven from command line I get following error:
type parameter V is not within its bound
The error is said to be on the first line of the sortMapByValues() method
I am using JDK 6 in my IDE and my maven compiler plugin is set to 1.6 as well.
Any tips appreciated
I guess it is because of:
V extends Comparable(insortMapByValues) andV extends Comparable<V>in classValueComparator.Change the first also to
V extends Comparable<V>