java - 使用匿名 Comparable 的 Collections.sort 出错

标签 java collections

首先,我不得不说,我确实广泛地搜索过这个问题的答案,但我找不到。

基本上我使用的格式与许多其他网站使用的格式完全相同,但我的 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

显然也欢迎所有其他建议。

我要么真的没有注意到一些明显的错误,要么这里可能存在一些更棘手的问题。

最佳答案

您不能对 HashMap 进行排序。这个不成立。可以对列表进行排序。数组可以排序。但不是 HashMap。

关于java - 使用匿名 Comparable 的 Collections.sort 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14789392/

相关文章:

java - 获取 wifi 流量统计 android

java - 使用 Jackson 使用 @RequestBody 进行 ajax 发布时出现 400 错误请求

java - 从数组列表中删除元素

java - 如何在java中查找arraylist的重复对象

java - 使用 java 8 流将子对象收集到 ParentObjectId 的 HashMap 中

java - JFace/SWT 文本区域

0到0.06之间的Java随机数

java - 处理多个文件的 Ant 任务

vector - 如何将集合中的多个元素传递给一个或多个元素可变的函数?

c# - Newsoft Json 反序列化为产品集合不起作用