我正在使用早期版本的 java 8 编写工作代码,我用它从列表中获取唯一值,但自从我升级到 JDK 66 后,它给了我一个错误
类型不匹配:无法转换List<Object>
to List<String>
List<String> instList = new ArrayList<String>();
while (res.next()) {
instList.add(res.getString("INST").toString());
}
List<String> instListF = instList.stream().distinct().collect(Collectors.toList());
Where res我从数据库中获取的结果集,不确定有什么问题吗?
嗯我也遇到过类似的错误Type mismatch: cannot convert from Set<Object> to Set<String>
最近。下面是代码片段:
public static void main(String[] args) {
String[] arr = new String[]{"i", "came", "i", "saw", "i", "left"};
Set<String> set = Arrays.asList(arr).stream().collect(Collectors.toSet());
System.out.println(set.size() + " distinct words: " + set);
}
Here is the screen shot for reference-:
![enter image description here](https://i.stack.imgur.com/0iMMH.png)
现在让我解释一下为什么我会收到此错误?在我的例子中,代码显示编译时错误,因为不匹配编译器版本在项目属性中。我选择了1.7但应该是1.8因为这个功能已经被添加到1.8.
因此,请注意以下几点:
- 已选择合适的JDKJava 构建路径. e.g. JDK 1.8在这种情况下。
- 合适的编译器版本必须在下面选择Java编译器 (如上面的截图所示) 在项目属性中。例如1.8
我希望这对你有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)