1

まず第一に、私はこれに対する答えを広範囲に探したと言わなければなりませんが、私はそれを見つけることができませんでした。

基本的に、他の多くのサイトが使用しているのとまったく同じ形式を使用していますが、私の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

他のすべてのアドバイスももちろん大歓迎です。

明らかな間違いに気づいていないか、ここでさらに難しい問題が発生している可能性があります。

4

2 に答える 2

6

HashMapを並べ替えることはできません。意味がない。リストは並べ替えることができます。配列を並べ替えることができます。しかし、HashMapではありません。

于 2013-02-09T15:06:51.450 に答える
5

Collection.sort関数はを受信するだけで、このMapインターフェイスを実装しません。アイテムを並べ替える必要がある場合は、の代わりにを使用することを検討してください。Collections#sortListMapMapTreeMapHashMap

于 2013-02-09T15:12:24.537 に答える