编译错误"不兼容的类型"

use*_*417 1 java iterator hashmap

我有编译错误:

Error: incompatible types: Object cannot be converted to String.
Run Code Online (Sandbox Code Playgroud)

在线 String buf = it.next();

public String getMostFrequentColor() {
    HashMap<String, Integer> colors = countColors();
    int count = 0;
    String mfcolour;
    Iterator it = colors.keySet().iterator();
    while (it.hasNext()) {
        String buf = it.next();
        if (colors.get(buf) > count) {
            count = colors.get(buf);
            mfcolour = buf;
        }
    }

    return mfcolour;
}
Run Code Online (Sandbox Code Playgroud)

我不知道为什么会发生这种情况.it.next()在我看来应该返回一个字符串.

Bub*_*tan 6

Iterator<String>而不是Iterator.

Iterator<String> it = colors.keySet().iterator();
Run Code Online (Sandbox Code Playgroud)