首先,我不得不說,我確實廣泛地搜索了一個答案,但我無法找到它。Collections.sort with anonymous Comparable提供了一個錯誤
基本上我使用與其他網站使用噸完全相同的格式,但我的NetBeans給這個錯誤。
我的代碼:
package ocr;
public class Property {
public static final int METHOD_NORMAL_SEARCH = 1;
public static final int METHOD_ADVANCED_SEARCH = 2;
private String property;
private ArrayList<String> names;
protected HashMap<String, Integer> values;
public Property(String propertyName, ArrayList<String> names) {
this.property = propertyName;
this.names = new ArrayList<>();
this.values = new HashMap<>();
for (String s : names) {
this.names.add(s);
}
}
public void add(String value, int method) {
values.put(value, method);
}
public int calculateSimilarity(String value) {
if (names.isEmpty()) {
throw new IllegalArgumentException("names cannot be empty.");
}
LevenshteinDistance ld = new LevenshteinDistance();
ArrayList<Integer> list = new ArrayList<>();
for (String n : names) {
list.add(ld.calculate(value, n));
}
Collections.sort(list);
return list.get(0);
}
public void search(File input) {
HashMap<String, Values> table = new HashMap<>(); //Maps word to {similarity, location in file}
CustomScanner scanner = new CustomScanner(input);
while (scanner.hasNext()) {
String next = scanner.next();
table.put(next, new Values(calculateSimilarity(next), scanner.getPosition()));
}
//Sorting on similarity
Collections.sort(table, new Comparator<Values>() {
@Override public int compare(Values val1, Values val2) {
return Integer.signum(val1.similarity - val2.similarity);
}
});
}
public void advancedSearch(File input) {
}
public void print() {
System.out.println("--" + property + "--");
for (Map.Entry<String, Integer> entry : values.entrySet()) {
System.out.println("Value: " + entry.getKey() + "/Method: " + entry.getValue());
}
}
private class Values {
private int similarity;
private int position;
public Values(Integer similarity, Integer position) {
this.similarity = similarity;
this.position = position;
}
public int getSimilarity() {
return similarity;
}
public int getPosition() {
return position;
}
}
}
這是我得到的錯誤:
C:\Users\Frank\Documents\NetBeansProjects\OCR\src\ocr\Property.java:62: error: no suitable method found for sort(HashMap<String,Property.Values>,<anonymous Comparator<Property.Values>>)
Collections.sort(table, new Comparator<Values>() {
method Collections.<T#1>sort(List<T#1>,Comparator<? super T#1>) is not applicable
(no instance(s) of type variable(s) T#1 exist so that argument type HashMap<String,Property.Values> conforms to formal parameter type List<T#1>)
method Collections.<T#2>sort(List<T#2>) is not applicable
(cannot instantiate from arguments because actual and formal argument lists differ in length)
where T#1,T#2 are type-variables:
T#1 extends Object declared in method <T#1>sort(List<T#1>,Comparator<? super T#1>)
T#2 extends Comparable<? super T#2> declared in method <T#2>sort(List<T#2>)
1 error
所有其他方面的建議顯然是歡迎以及。
我要麼真的沒有注意到一些明顯的錯誤,要麼會在這裏發生一些更難的問題。
是否有可能讓我更深入瞭解如何使它成爲TreeMap,只需將HashMap更改爲TreeMap就可能是一種太簡單的思考方式。 另外,你們(人們回答我)會如何解決這樣的問題? 你有一個表: 鍵(字符串) - 值1(整數) - 值2(整數) 而你想要它在Value1上排序,同時明顯保留Key-Value1-Value2之間的關係。 任何幫助,這將不勝感激。 – skiwi 2013-02-09 15:19:38
對不起,它使用編輯令人討厭,但我不能迴應我自己的問題呢:旁註:奇怪,我無法找到自己,你不能排序HashMap?我的意思是既沒有錯誤,也沒有NetBeans,也沒有谷歌的結果讓我相信這可能是問題。 – skiwi 2013-02-09 15:19:58
最後編輯:我剛把HashMap改成ArrayList ,其中Values的形式爲{String,Integer,Integer},這似乎表現得很好。謝謝你所有的答案。 –
skiwi
2013-02-09 15:23:35