I have a generic type K, and I want to verify whether K implements interface Comparable or not. Here is my code:
public class HashMapVectorSimilarity<K> {
static public <K> double calculateSimilarity(HashMap<K, Double> hashMap1, HashMap<K, Double> hashMap2) {
if (Comparable.class.isAssignableFrom(K)) {//
}
}
But the code doesn't work. I don't want to limit all K to implementing Comparable. I have another TreeMapVectorSimilarity class already. If K implements interface Comparable,I will return the result using TreeMapVectorSimilarity class instead.
How do you think I can solve this problem? Thanks.