Home » Java » Java Sorting parameters in a new hashmap than returning to main

Java Sorting parameters in a new hashmap than returning to main

Posted by: admin August 11, 2018 Leave a comment

Questions:

I’m working on a KNN project and I’m trying to sort the euclidean distances I’ve calculated in a new hashmap alongside with an index, than take them back to my main. Here is my code for sorting the distances. However, public int compare() function ONLY returns int, I cannot set it as public double compare() and since all my distances are doubles, I cannot use this function. I will appreciate any help, thank you very much.

HashMap<Integer, KnnBundle> knnBundleHashMap = new HashMap<Integer, knnBundle>();

// cnt is the size
for(int i = 0; i< cnt; i++){
knnBundleHaspMap.put(i, newKnnBundle(xarray[i], yarray[i], classes[i], euclid[i]);
}

// not yet sorted
List<KNNBundle>sortedEuclid = new ArrayList<knnBundle>(knnBundleHaspMap.values());

Collections.sort(sortedEuclid, new Comparator<KNNBundle>() {

    public int compare(KNNBundle o1, KNNBundle o2) {
        return o1.getEuclid2() - o2.getEuclid2();
    }
});
Answers: